我的HMI由2个按钮组成:Start和Stop,还有一个字段QStatusBar,用于将信息从线程打印到GUI。当我做“开始”时,我启动一个线程,我可以用停止按钮中断。 我想要做的是在线程中的某个点打开一个弹出窗口,以便在线程暂停时为用户提供选择。PyQt:从GUI发送信息到线程
from PyQt4 import QtCore, QtGui
import sys
import os
import time
from PyQt4.QtGui import QApplication, QDialog, QListWidgetItem, QListWidget, QIcon
from test_bench_tool2 import Ui_MainWindow
from popup import Ui_popup
"""Class poppup window(continue/quit)"""
class MyPopup(QtGui.QDialog):
def __init__(self,type):
super(MyPopup, self).__init__()
self.res = 0
self.type=type
self.ui = Ui_popup()
self.ui.setupUi(self)
QtCore.QObject.connect(self.ui.Quit,QtCore.SIGNAL("clicked()"),self.showDialogstop)
QtCore.QObject.connect(self.ui.Continue,QtCore.SIGNAL("clicked()"),self.showDialogcontinue)
def showDialogcontinue(self):
self.res=1
self.close()
def showDialogstop(self):
self.res=0
self.close()
class MyThread(QtCore.QThread):
Statuschanged = QtCore.pyqtSignal(str)
popupmodechanged = QtCore.pyqtSignal(str)
def __init__(self, parent=None):
super(MyThread, self).__init__(parent=parent)
self.Status_auto = ''
self.dialog = MyPopup('toto')
self.Status_auto +=''
self.popup=''
self.answer=''
def run(self):
result = self.get_result()
def get_result(self,):
empty_result = []
self.popup='Vn'
self.popupmodechanged.emit((self.popup))
self.Status_auto +='\n\nMeasures finished!!'
self.Statuschanged.emit((self.Status_auto))
results=[]
#do things to calculate results
return(results)
class Main(QtGui.QDialog):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
# Connect the Buttons
QtCore.QObject.connect(self.ui.Start,QtCore.SIGNAL("clicked()"),self.Start)
QtCore.QObject.connect(self.ui.Stop,QtCore.SIGNAL("clicked()"),self.Stop)
self.__thread = MyThread(self)
self.__thread.Statuschanged.connect(self.ui.report_auto.setText)
self.__thread.popupmodechanged.connect(self.open_popup)
def open_popup(self):
self.__thread.dialog.__init__(self.__thread.dialog.type)
self.__thread.dialog.exec_()
def Start(self):
global tableRx, tableTx
self.ui.report_auto.setText('test in process...')
self.__thread.start()
def Stop(self):
self.ui.report_auto.setText('test canceled')
if self.__thread.isRunning():
self.__thread.terminate()
def main():
app = QtGui.QApplication(sys.argv)
window = Main()
window.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
我怎么能得到从弹出选择继续线程的结果呢?我曾试图把get_result()函数,这样
if self.dialog.res == 1:
print "continue"
self.dialog.close()
#do things
else:
self.dialog.close()
测试,但我不能从GUI得到结果的线程。我如何从GUI获取信息到我的线程?我有tu使用队列吗?
你不应该建立在一个单独的QThread一个GUI(你应该看到警告,当你这样做)。相反,在QThread中进行计算,等待线程完成,然后从主GUI线程中打开弹出窗口。 – sebastian
对不起,但这不是我想要做的,弹出窗口必须出现在线程中间,但是谢谢你的建议 –