How to Create Python GUI Button in PyQt6

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.



First of all we need to import required module and classes from Python PyQt6.



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.


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.




This is the complete code for this article




Run the code and this will be the result

How to Create Python GUI Button in PyQt6
How to Create Python GUI Button in PyQt6



Learn More on Python GUI

Leave a Comment