如何解决Python QThread 与主程序通信
我需要在主程序 (QMainWindow) 的文本区域中显示某个线程的一些信息,但我找不到正确的说明来执行此操作
线程在执行过程中会显示一些信息,但我不明白如何将变量传递给主程序 (QMain) 并显示出来。
你有什么建议可以帮助我吗?
感谢里斯托夫
import sys
from PySide6.QtCore import *
from PySide6.QtGui import *
from PySide6.QtWidgets import *
import time
class MainWindow(QMainWindow):
process = 0
def __init__(self):
super().__init__()
self.setup()
def setup(self):
QtWidgets = QWidget(self)
# traitement de la fenetre principale
self.setGeometry(200,200,400,400) # dimension de la fenetre
self.setwindowTitle('AOT Automate v2')
# Traitement des actions
startAction = QAction(QIcon("exit.png"),"&Start / Stop",self)
startAction.setShortcut("Ctrl+S")
startAction.setStatusTip("Lancer les processus")
startAction.triggered.connect(self.Menu_Start)
exitaction = QAction(QIcon("exit.png"),"&Exit",self)
exitaction.setShortcut("Ctrl+Q")
exitaction.setStatusTip("Quitter l'application")
exitaction.triggered.connect(qApp.quit)
# creation des menus
ExitMenu = self.menuBar().addMenu("&Menu");
ExitMenu.addAction(startAction);
ExitMenu.addAction(exitaction);
self.textbrowser = QTextbrowser()
self.textbrowser.setStyleSheet('font-size: 12px')
self.setCentralWidget(self.textbrowser)
#self.textbrowser.moveCursor(QTextCursor.Start)
self.textbrowser.append("Message from the tread must be displayed in this field")
self.worker = WorkerThread()
# Barre outil du bas
self.statusBar().showMessage("Ready",2000)
self.show()
def Menu_Start(self):
if self.process == 0:
print("entre")
self.worker.start()
self.process = 1
else:
print("exit")
self.worker.terminate()
self.process = 0
class WorkerThread(QThread):
def run(self):
i = 2
while i != 0:
print("loop of messages from the tread must be displayed in the MainProgram")
def run():
app = QApplication(sys.argv)
ex = MainWindow()
app.exec_()
if __name__ == '__main__':
run()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。