In this Python PyQt6 article we are going to learn about Creating GUI Applications with Python and PyQt6, so GUI development is an important part of modern software development. because it allows users to interact with software in more simple and user friendly way, and when we are talking about Python, there are different options for building GUI Applications with Python, and Python provides different GUI Frameworks for this purpose, in the article we are going to talk about Creating Python GUI Applications with PyQt6, which is Python binding for popular Qt Application Framework.
Before we start building our Python GUI Applications with PyQt6 we need to make sure that we have necessary tools. we need Python, PyQt6 and an Integrated Development Environment (IDE) to write our code. there are many IDEs available, but for this tutorial we are going to use PyCharm IDE, which have free and also commercial versions.
Python installation is easy and simple, you can visit official website of Python and download the latest version. once Python is installed, we can install PyQt6 using pip like this:
1 |
pip install PyQt6 |
Now let’s create our simple Python GUI application that displays a window with button. When we click the button , it will display a message box.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import sys from PyQt6.QtWidgets import QApplication, QWidget, QPushButton, QMessageBox class MyWindow(QWidget): def __init__(self): super().__init__() self.MyUI() def MyUI(self): self.setWindowTitle("GeeksCoders.com") self.setGeometry(100, 100, 300, 200) button = QPushButton("Click me", self) button.move(100, 100) button.clicked.connect(self.showDialog) def showDialog(self): msgBox = QMessageBox() msgBox.setText("Hello from GeeksCoders.com") msgBox.exec() |
In the above example we have import necessary modules. we need sys to exit the application, QApplication to create the application, QWidget to create main window, QPushButton to create button and QMessageBox to display message box.
after that we have created new class called MyWindow that extends from QWidget. this class represents the main window of our application. in the constructor, we call the MyUI() method, which initializes the user interface.
In the MyUI method we set window title and size using the setWindowTitle and setGeometry methods. also we have created a button using QPushButton constructor and move it to the center of the window using the move method. also we connected clicked signal of the button to the showDialog method.
In showDialog() method, we have created new message box using the QMessageBox constructor and set its text using the setText method. and after that we display the message box using the exec method.
This is our 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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
import sys from PyQt6.QtWidgets import QApplication, QWidget, QPushButton, QMessageBox class MyWindow(QWidget): def __init__(self): super().__init__() self.MyUI() def MyUI(self): # define a method to initialize the user # interface of the window self.setWindowTitle("GeeksCoders.com") # set the window title self.setGeometry(100, 100, 300, 200) # set the window size and position button = QPushButton("Click me", self) # create a button with the label "Click me" and add it to the window button.move(100, 100) # move the button to position (100, 100) within the window button.clicked.connect(self.showDialog) # connect the button's clicked signal to the showDialog method def showDialog(self): # define method to display a message box msgBox = QMessageBox() # create new QMessageBox object msgBox.setText("Hello from GeeksCoders.com") # set the text of the message box msgBox.exec() # display messag box if __name__ == "__main__": # define the main program entry point app = QApplication(sys.argv) # create a new QApplication object window = MyWindow() # create a new MyWindow object #show the window window.show() sys.exit(app.exec()) |
Run the code and this is the result