PyQt编程工作子线程与GUI线程示例

Posted by FanHao on 2024-01-20

基于PySide6下的Gui编程示例

  • 工作子线程,接收信号后开始工作,run()写程序后台运行逻辑
  • GUI程序,死循环连接控件信号槽函数
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
import sys
import time
from PySide6.QtWidgets import QMainWindow, QApplication
from PySide6.QtCore import QThread, Signal
from common.gui import Ui_Form


class WorkThread(QThread):
trigger = Signal(str)
# work子线程
def __init__(self, parent=None) -> None:
super().__init__(parent)

def run(self):
for i in range(1,16):
time.sleep(1)
self.trigger.emit(str(i))


class MianWin(QMainWindow, Ui_Form):
# 主gui程序,死循环
def __init__(self, parent=None) -> None:
super().__init__(parent)
self.setupUi(self)
self.thread = WorkThread(self)
self.runButton.clicked.connect(self.start_run)

def display(self, i):
self.listWidget.addItem(str(i))

def start_run(self):
self.thread.start()
self.thread.trigger.connect(self.display)


if __name__ == "__main__":
app = QApplication(sys.argv)
window = MianWin()
window.show()
sys.exit(app.exec())