1 回答
TA贡献1804条经验 获得超3个赞
耗时的任务必须在辅助线程中执行,以避免阻塞 GUI,并且必须使用信号将信息发送到主线程。
import threading
import time
from PyQt5 import uic, QtCore, QtWidgets
class Task(QtCore.QObject):
messageChanged = QtCore.pyqtSignal(str)
def start(self):
threading.Thread(target=self._execute, daemon=True).start()
def _execute(self):
for a in range(20):
print("Print index: " + str(a))
self.messageChanged.emit("Print index: " + str(a))
time.sleep(2)
def main():
app = QtWidgets.QApplication([])
W_exemplogui = uic.loadUi("testegui.ui")
task = Task()
W_exemplogui.pushButton.clicked.connect(task.start)
task.messageChanged.connect(W_exemplogui.textEdit.append)
W_exemplogui.show()
app.exec_()
if __name__ == "__main__":
main()
添加回答
举报