Python中界面阻塞情况的解决方案
# 建议所有的界面更新动作都放在主线程中,否则textEdit控件可能会出现程序崩溃的问题
from PySide2.QtWidgets import QApplication
from PySide2.QtUiTools import QUiLoader
from PySide2.QtCore import Signal, QObject
from threading import Thread
# 定义信号源
class Mysignals(QObject):
# 定义一个信号
text_print = Signal(str)
class Stat:
def __init__(self):
self.ui = QUiLoader().load('ui/test.ui')
self.ui.button.clicked.connect(self.handleCalc)
self.ms = Mysignals()
# 监听自定义信号
self.ms.text_print.connect(self.update_ui)
def update_ui(self, str1):
self.ui.textedit.setPlainText(str1)
def handleCalc(self):
def download():
info = ''
for i in range(5):
info += f'正在下载-----{i}\n'
# 发射自定义信号
self.ms.text_print.emit(info)
sleep(1)
t1 = Thread(target=download)
t1.start()
app = QApplication()
stat = Stat()
stat.ui.show()
app.exec_()
------学习贵在分享,贵在记录,贵在总结。