In this Python PyQt6 tutorial we want to learn How to Create Python GUI Button in PyQt6, so PyQt6 is one of the most popular Python GUI Framework, and it is used for building GUI applications in Python programming language.
First of all make sure that you have already installed PyQt6, if you don’t have PyQt6, you can use pip for the installation.
1 |
pip install PyQt6 |
First of all we need to import required module and classes from Python PyQt6.
1 2 |
import sys from PyQt6.QtWidgets import QApplication, QMainWindow, QLabel, QPushButton |
After that we will create a class that extends from QMainWindow class, which will serve as our main application window. inside this class we are going to define the button and label widgets. we will also set up the necessary connections for button click events.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("Python GUI Button") self.setGeometry(300, 300, 400, 200) self.label = QLabel(self) self.label.setGeometry(100, 50, 200, 50) self.button = QPushButton("Click Me", self) self.button.setGeometry(150, 100, 100, 30) self.button.clicked.connect(self.button_clicked) def button_clicked(self): self.label.setText("PyQt6 Tutorial in Geekscoders.com") |
Now let’s talk about above code one by one.
In the MainWindow class, we override __init__ method to perform some initial setup. also we set the window title and dimensions using setWindowTitle and setGeometry methods.
After that we creates a QLabel widget called label and position it at coordinates (100, 50) with a width of 200 and height of 50 pixels. this label will display the clicked result.
We also create a QPushButton widget called button and position it at coordinates (150, 100) with a width of 100 and height of 30 pixels. we connect the clicked signal of the button to the button_clicked slot, which will be called when the button is clicked.
Inside the button_clicked method, we set the text of the label widget using the setText method.
For runing this application, we need to create an instance of the QApplication class and pass the command line arguments to it. we also creates an instance of the MainWindow class and make it visible using the show method.
1 2 3 4 5 |
if __name__ == '__main__': app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec()) |
This is the complete code for this article
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import sys from PyQt6.QtWidgets import QApplication, QMainWindow, QLabel, QPushButton class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("Python GUI Button") self.setGeometry(300, 300, 400, 200) self.label = QLabel(self) self.label.setGeometry(100, 50, 200, 50) self.button = QPushButton("Click Me", self) self.button.setGeometry(150, 100, 100, 30) self.button.clicked.connect(self.button_clicked) def button_clicked(self): self.label.setText("PyQt6 Tutorial in Geekscoders.com") if __name__ == '__main__': app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec()) |
Run the code and this will be the result
