Sunday, November 6, 2022

PyQt6 Progress Bar Enhancement

Today, I made an effort to enhance the PyQt6 Progress Bar I posted last March to include multithreading features. This is needed to maintain the responsiveness of any application. The old program which can still be accessed here, would normally freeze the whole window but with the multithreading feature, other areas of the window would be operation while looping in the progressbar is on going.

For detailed explanation of this topic, you may refer to this youtube video:

I also used this feature in the python serial I am creating with Pyqt6 user interface(I am currently enhancing the python program in this post.) . Multithreading in this particular application which involves serial communication requiring constant monitoring of the port for new data. Without multithreading, all areas of the application would freeze therefore making the gui impractical.

Here is the sample screenshot:



and here is the code:

 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import sys
import time
from PyQt6.QtWidgets import QLabel, QSlider, QApplication,  QWidget, QPushButton, QProgressBar, QMessageBox
from PyQt6.QtCore import Qt, QThread, pyqtSignal

TIME_LIMIT = 100

class Window(QWidget):

    def __init__(self):
        super(Window, self).__init__()

        self.initUI()

    def initUI(self):

        self.progress = QProgressBar(self)
        self.progress.setGeometry(25, 35, 305, 20)
        self.progress.setMaximum(100)
        self.progress.setStyleSheet("QProgressBar::chunk "
                          "{"
                          "background-color: red;text-align: center"
                          "}")
        self.progress.setAlignment(Qt.AlignmentFlag.AlignCenter)
        
        pb3 = QPushButton('Process', self)
        pb3.setGeometry(50, 100, 250, 30)
        pb3.setStyleSheet('QPushButton {background-color: #2F569B; color: #d4d4d4;}')
        pb3.clicked.connect(self.onClick_pb3)

        self.sld = QSlider(Qt.Orientation.Horizontal, self)
        self.sld.setFocusPolicy(Qt.FocusPolicy.NoFocus)
        self.sld.setGeometry(30, 175, 200, 30)
        self.sld.valueChanged[int].connect(self.changeValue)
        self.sld.setSingleStep(2)

        self.label = QLabel(self)       
        self.label.setGeometry(30, 150, 200, 30)
        self.label.setAlignment(Qt.AlignmentFlag.AlignCenter)

        self.setGeometry(25, 45, 350, 250)
        self.setWindowTitle('Post 5')
        self.show()
        
    def changeValue(self, value):

        self.label.setText(str(value))
 
    def onClick_pb3(self):
       self.worker = WorkerThread()
       self.worker.start()
       self.worker.finished.connect(self.evt_worker_finished)
       self.worker.update_progress.connect(self.evt_update_progress)
       #count = 0
       #while count < TIME_LIMIT:
       #     count += 1
       #     time.sleep(0.5)
       #     self.progress.setValue(count)
    def evt_worker_finished(self):
       QMessageBox.information(self, "Done!", "Worker thread complete")

    def evt_update_progress(self, val):
       self.progress.setValue(val)
       
class WorkerThread(QThread):
    
    update_progress = pyqtSignal(int)
    def run(self):
        count = 0
        while count < 100:
            #print(x)
            count += 1
            time.sleep(0.5)
            self.update_progress.emit(count)
def main():

    app = QApplication(sys.argv)
    ex = Window()
    sys.exit(app.exec())


if __name__ == '__main__':
    main()


No comments:

Post a Comment