This article provides an example of opening a window and using a button in response to PyQt5, which is the basis for using the other components. The highlight of Qt is that it is a cross-platform C++ development kit and covers the integration for compatibility with languages such as PyQt, “The python binding for the Qt cross-platform C++ framework” developed by Riverbank Computing Limited, currently released in PyQt 6.1.1 (2021-06-29).
Install the package pyqt5 and pyqt5-sip with the following commands.
pip install pyqt5 pyqt5-sip
pip3 install pyqt5 pyqt5-sip
An example of a basic program is as follows.
#kmqt5-1.py # -*- coding: UTF-8 -*- import sys from PyQt5.QtWidgets import QApplication, QWidget from PyQt5.QtGui import QIcon class MyApp(QWidget): def __init__(self, title, w, h): super().__init__() self.setWindowTitle(title) self.setGeometry(0, 0, w, h) self.show() if __name__ == '__main__': app = QApplication(sys.argv) my_app = MyApp("PyQt5: สวัสดีคิวท์5", 640, 480) sys.exit(app.exec_())
From the sample code, you will see that the programming structure consists of the following three parts.
This first section is for importing the required libraries. This includes QtWidget, QApplication and QIcon.
The second part is the main window class in __init__( ), the constructor or initiator that acts as the first function when the class was created as an object. It defines the name of the window header as it gets from the argument and sets the window’s width and height to w,h. Finally, the window is displayed.
The third part is the main programming part of the programmer. An app object is created from Qapplication, then my_app is created from the created class. Finally, run it with app.exec_().
When running the program with the commands below, you will see the result as shown in Figure 1.
Create Status Bar
The status bar is a type of window that is a sub-window of a window created. They are usually located at the bottom of the main window. In the example code kmqt5-2.py, QMainWindow is used instead of the Widget window as in kmqt5-1.py. Since the status bar is a widget-level window, it must have a parent window class as a controller for window under the written program.
#kmqt5-2.py # -*- coding: UTF-8 -*- import sys from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow from PyQt5.QtGui import QIcon class MyApp(QMainWindow): def __init__(self, title, w=640, h=480): super().__init__() self.setWindowTitle(title) self.setGeometry(0, 0, w, h) self.statusBar().showMessage('status: สถานะอยู่ตรงนี้!') self.show() if __name__ == '__main__': app = QApplication(sys.argv) my_app = MyApp("PyQt5: สวัสดีคิวท์5 มีสเตตัสบาร์แล้วนะ") sys.exit(app.exec_())
From kmqt5-2.py, when running it will show status bar as shown in picture 2.
Buttons and responses
Creating a button object requires the QPushButton class, which is another layer under the QtWidgets class. The process of creating a button consists of 5 steps:
- Create a button object from the QPushButton class and define the text to be displayed on the generated button or use setText() to change the text later.
- Set a tooltip or text that floats on a button when the user moves the mouse position over the button with setToolTip().
- Set the position of the buttons in the window with move(x, y).
- Establish a link between the keypress event and the response function with clicked[bool].connect( Response function ).
- Create a function that responds to button events.
Example code kmqt5-3.py to create an active button. When working, the results will be as shown in Figures 3 and 4.
# -*- coding: UTF-8 -*- import sys from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow, QPushButton from PyQt5.QtGui import QIcon, QColor class MyApp(QWidget): def __init__(self, title, w=640, h=480): super().__init__() self.setWindowTitle(title) self.setGeometry(0, 0, w, h) self.status = False self.my_button = QPushButton("Click-->Yellow::คลิกเพื่อเปลี่ยนเป็นสีเหลือง", self) self.my_button.setToolTip("เจอปุ่มก็ต้องกดสินะ!") self.my_button.move( 100, 100 ) self.my_button.clicked[bool].connect(self.on_clicked_my_button) self.show() def on_clicked_my_button(self): source = self.sender() if self.status == False: self.status = True self.setStyleSheet("background-color:yellow;") self.my_button.setText("Click-->White::คลิกเพื่อเปลี่ยนเป็นสีขาว") else: self.status = False self.setStyleSheet("background-color:white;") self.my_button.setText("Click-->Yellow::คลิกเพื่อเปลี่ยนเป็นสีเหลือง") if __name__ == '__main__': app = QApplication(sys.argv) my_app = MyApp("PyQt5: สวัสดีคิวท์5 มีปุ่มกด ก็ต้องกดปุ่ม") sys.exit(app.exec_())
From the three example code, it can be seen that programming a GUI with Python to using Qt is not complicated. and supports the use of Thai language without having to modify the code further. In this article, you have learned to create a window, using the status bar and the uses of buttons to respond to users. We believe that it will be a good base for continuing to learn programming with PyQt5 or to switch to the newer version of PyQt6. Finally, have fun programming.
If you want to discuss or talk with us, feel free to leave comments below!
(C) 2020-2021, By Jarut Busarathid and Danai Jedsadathitikul