A Beginner's Guide to Using PyQT 6 for GUI Development
Introduction PyQt6 is a popular Python binding for the Qt cross-platform application framework. It allows Python developers to create graphical user interfaces for desktop applications easily. PyQt6 is a comprehensive set of Python bindings for the Qt toolkit. In this tutorial, we will explore the basics of PyQt6 by creating a simple desktop application.
Prerequisites Before starting with the tutorial, you should have the following:
Python 3.6 or later PyQt6 package installed
pip install PyQt6
Setting up the PyQt6 Application We will start by creating a simple PyQt6 application that will display a window with a label. To do that, we will import the required modules and create a QApplication object.
import sys
from PyQt6.QtWidgets import QApplication, QLabel, QWidget, QPushButton, QVBoxLayout
app = QApplication(sys.argv)
layout = QVBoxLayout()
widget = QWidget()
widget.setLayout(layout)
widget.show()
label = QLabel("Hello, PyQt6!")
layout.addWidget(label)
sys.exit(app.exec())
In the above code, we first imported the required modules: sys, QApplication, QLabel, and QWidget. Next, we created an instance of the QApplication class and passed the sys.argv list as an argument. This list contains the command-line arguments passed to the script.
Next, we created a QWidget object named widget. A QWidget is a top-level widget that provides a window container. Then, we created a QLabel object named label and set its parent to the window widget. Finally, we showed the window widget and executed the application.
We also created an object of class QVBoxLayout that is responsible of vertical layout of widget and then we set that as layout of our widget object.
Adding a Button Now, let’s add a button to the window. To do that, we will create a QPushButton object and add it to the window widget.
import sys
from PyQt6.QtWidgets import QApplication, QLabel, QWidget, QPushButton, QVBoxLayout
app = QApplication(sys.argv)
layout = QVBoxLayout()
widget = QWidget()
widget.setLayout(layout)
widget.show()
label = QLabel("Hello, PyQt6!")
layout.addWidget(label)
button = QPushButton("Click Me!")
layout.addWidget(button)
sys.exit(app.exec())
In the above code, we imported the QPushButton class and created an instance of it named button. We set the parent of the button to the window widget.
Handling Button Clicks Now, let’s add functionality to the button by connecting it to a function that will be called when it is clicked. To do that, we will create a new function and connect it to the clicked signal of the button.
import sys
from PyQt6.QtWidgets import QApplication, QLabel, QWidget, QPushButton, QVBoxLayout
def on_button_click():
global label
label.setText("Button Clicked")
app = QApplication(sys.argv)
layout = QVBoxLayout()
widget = QWidget()
widget.setLayout(layout)
widget.show()
label = QLabel("Hello, PyQt6!")
layout.addWidget(label)
button = QPushButton("Click Me!")
button.clicked.connect(on_button_click)
layout.addWidget(button)
sys.exit(app.exec())
In the above code, we created a new function named on_button_click that sets the text of the label widget to “Button Clicked”. We then connected the clicked signal of the button widget to this function using the connect method.
Conclusion In this tutorial, we learned how to create a simple PyQt6 application that displays a window with a label and a button. We also learned how to handle button clicks by connecting the clicked signal to a function. This is just the beginning of what you can do with PyQt6. With PyQt6, you can create powerful and complex GUI applications that run on multiple platforms.