PyQt

Boiler Plate

Below is boiler plate code for the controller when you are working with a PyQt UI file generated from QT Designer.

 1import sys
 2from PyQt6.QtWidgets import QApplication, QMainWindow
 3from ui_mainwindow import Ui_MainWindow
 4
 5
 6class MainWindow(QMainWindow):
 7    def __init__(self):
 8        """
 9        Initialise window
10        """
11        
12        super().__init__()
13
14        # ---- setup GUI elements ---- 
15        self.main_win = QMainWindow()
16        self.ui = Ui_MainWindow()
17        self.ui.setupUi(self.main_win)
18        
19        # ---- initialise variables ---- 
20        
21        
22        # ---- initialise GUI with starting values ----
23        self.signals()
24
25    def show(self):
26        """
27        Displays main window
28        """
29        self.main_win.show()
30
31    def signals(self):
32        """
33        Connects the UI buttons to the corresponding functions (see slots)
34        """
35        
36        ...
37        
38    # ---- slots ----
39
40
41if __name__ == '__main__':
42    app = QApplication(sys.argv)
43    main_win = MainWindow()
44    main_win.show()
45    sys.exit(app.exec())

Creating a timer

Below is code for a simple timer app using PyQt with code generated from QT Designer.

Controller (main) file

 1import sys
 2from PyQt6.QtWidgets import QApplication, QMainWindow
 3from PyQt6.QtCore import QTimer
 4from ui_timer import Ui_MainWindow
 5
 6
 7class MainWindow(QMainWindow):
 8    def __init__(self):
 9        """
10        Initialise window
11        """
12
13        super().__init__()
14
15        # ---- setup GUI elements ---- 
16        self.main_win = QMainWindow()
17        self.ui = Ui_MainWindow()
18        self.ui.setupUi(self.main_win)
19        
20        # ---- initialise variable ---- 
21        self.timer = QTimer(self)
22        self.time = 0
23        
24        # ---- initialise GUI with starting values ----
25        self.signals()
26
27    def show(self):
28        """
29        Displays main window
30        """
31        self.main_win.show()
32
33    def convert_time(self, seconds):
34        """
35        Returns a m:ss represenation of the seconds
36        """
37        mins = str(seconds // 60)
38        secs = f"0{str(seconds % 60)}" if seconds % 60 < 10 else str(seconds % 60)
39        return f"{mins}:{secs}"
40
41    def signals(self):
42        """
43        Connects the UI buttons to the corresponding functions (see slots)
44        """
45        self.timer.timeout.connect(self.update_time)
46        self.ui.start_btn.clicked.connect(self.start_timer)
47        self.ui.stop_btn.clicked.connect(self.stop_timer)
48        self.ui.reset_btn.clicked.connect(self.reset_timer)
49        
50    # ---- slots ----
51    def update_time(self):
52        """
53        Changes the value of the timer on display
54        """
55        self.time += 1
56        self.ui.time_lb.setText(self.convert_time(self.time))
57        
58    def start_timer(self):
59        """
60        Starts the timer running
61        """
62        self.timer.start(1000)
63        
64    def stop_timer(self):
65        """
66        Stops the timer
67        """
68        self.timer.stop()
69        
70    def reset_timer(self):
71        """
72        Stops the timer and resets it to 0
73        """
74        self.timer.stop()
75        self.time = 0
76        self.ui.time_lb.setText(self.convert_time(self.time))
77
78if __name__ == '__main__':
79    app = QApplication(sys.argv)
80    main_win = MainWindow()
81    main_win.show()
82    sys.exit(app.exec())

View (UI) file

This has been generated from a QT Designer file.

 1# Form implementation generated from reading ui file 'timer.ui'
 2#
 3# Created by: PyQt6 UI code generator 6.7.0
 4#
 5# WARNING: Any manual changes made to this file will be lost when pyuic6 is
 6# run again.  Do not edit this file unless you know what you are doing.
 7
 8
 9from PyQt6 import QtCore, QtGui, QtWidgets
10
11
12class Ui_MainWindow(object):
13    def setupUi(self, MainWindow):
14        MainWindow.setObjectName("MainWindow")
15        MainWindow.resize(500, 250)
16        self.centralwidget = QtWidgets.QWidget(parent=MainWindow)
17        self.centralwidget.setObjectName("centralwidget")
18        self.verticalLayoutWidget = QtWidgets.QWidget(parent=self.centralwidget)
19        self.verticalLayoutWidget.setGeometry(QtCore.QRect(0, 0, 501, 193))
20        self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
21        self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
22        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
23        self.verticalLayout.setObjectName("verticalLayout")
24        self.time_lb = QtWidgets.QLabel(parent=self.verticalLayoutWidget)
25        font = QtGui.QFont()
26        font.setPointSize(100)
27        self.time_lb.setFont(font)
28        self.time_lb.setAlignment(QtCore.Qt.AlignmentFlag.AlignRight|QtCore.Qt.AlignmentFlag.AlignTrailing|QtCore.Qt.AlignmentFlag.AlignVCenter)
29        self.time_lb.setObjectName("time_lb")
30        self.verticalLayout.addWidget(self.time_lb)
31        self.horizontalLayout = QtWidgets.QHBoxLayout()
32        self.horizontalLayout.setObjectName("horizontalLayout")
33        self.start_btn = QtWidgets.QPushButton(parent=self.verticalLayoutWidget)
34        self.start_btn.setObjectName("start_btn")
35        self.horizontalLayout.addWidget(self.start_btn)
36        self.stop_btn = QtWidgets.QPushButton(parent=self.verticalLayoutWidget)
37        self.stop_btn.setObjectName("stop_btn")
38        self.horizontalLayout.addWidget(self.stop_btn)
39        self.reset_btn = QtWidgets.QPushButton(parent=self.verticalLayoutWidget)
40        self.reset_btn.setObjectName("reset_btn")
41        self.horizontalLayout.addWidget(self.reset_btn)
42        self.verticalLayout.addLayout(self.horizontalLayout)
43        MainWindow.setCentralWidget(self.centralwidget)
44        self.menubar = QtWidgets.QMenuBar(parent=MainWindow)
45        self.menubar.setGeometry(QtCore.QRect(0, 0, 500, 21))
46        self.menubar.setObjectName("menubar")
47        MainWindow.setMenuBar(self.menubar)
48        self.statusbar = QtWidgets.QStatusBar(parent=MainWindow)
49        self.statusbar.setObjectName("statusbar")
50        MainWindow.setStatusBar(self.statusbar)
51
52        self.retranslateUi(MainWindow)
53        QtCore.QMetaObject.connectSlotsByName(MainWindow)
54
55    def retranslateUi(self, MainWindow):
56        _translate = QtCore.QCoreApplication.translate
57        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
58        self.time_lb.setText(_translate("MainWindow", "0:00"))
59        self.start_btn.setText(_translate("MainWindow", "Start"))
60        self.stop_btn.setText(_translate("MainWindow", "Stop"))
61        self.reset_btn.setText(_translate("MainWindow", "Reset"))
62
63
64if __name__ == "__main__":
65    import sys
66    app = QtWidgets.QApplication(sys.argv)
67    MainWindow = QtWidgets.QMainWindow()
68    ui = Ui_MainWindow()
69    ui.setupUi(MainWindow)
70    MainWindow.show()
71    sys.exit(app.exec())