我有一个PyQT gui,它有一个gui线程和24个“测试器线程”。他们工作得很好,但是当我关闭gui时,他们似乎仍然熬夜。我怎样才能优雅地关闭线程以避免python崩溃?
#!/usr/bin/python
# Standard Lib
from datetime import datetime
import logging
import os
import random
import sys
import time
# Third Party
from PyQt4 import QtGui, QtCore
# Local Kung Fu
stuff
class SSITesterThread(QtCore.QThread):
# vars for updating gui using signals
updateText = QtCore.pyqtSignal(str)
updateColor = QtCore.pyqtSignal(str)
updateSN = QtCore.pyqtSignal(str)
def __init__(self, thread_number, port, path, parent=None):
super(SSITesterThread, self).__init__(parent)
self.delay = random.random()
def run(self):
self.ssitester()
def ssitester(self):
# stuff
class SSITestSuiteGUI(QtGui.QMainWindow):
def __init__(self, parent=None):
self._threads = []
QtGui.QWidget.__init__(self, parent)
# Init class from template and paths
self.launch_tester_threads()
def init_gui_nodes(self, com_ports_list):
for num, port, in zip(range(1, 25), range(0, 24)):
label = getattr(self.ui, 'com_{}'.format(num))
label.setText("COM Port: {}".format(com_ports_list[port]["COM"]))
def launch_tester_threads(self):
logging.info("Spinning up threads...")
for num, com_port_chunk in zip(range(1, 25), self.com_ports_list):
tester_thread = SSITesterThread(thread_number=num, port=com_port_chunk["COM"], path=self.vc_test_path)
# get a reference to the associated textbox somehow...
status_box = getattr(self.ui, 'status_{}'.format(num))
tester_thread.updateText.connect(status_box.setText)
status_box = getattr(self.ui, 'status_{}'.format(num))
tester_thread.updateColor.connect(status_box.setStyleSheet)
sn_label = getattr(self.ui, 'sn_{}'.format(num))
tester_thread.updateSN.connect(sn_label.setText)
sn_label.setText("S/N: None")
tester_thread.start()
self._threads.append(tester_thread)
logging.info("Ready for tests.")
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
test_suite = SSITestSuiteGUI()
test_suite.show()
# Close app only when window is closed.
sys.exit(app.exec_())
但得到了一个错误:
attributeerror: 'function' object has no attribute '__pyqtSignature__'
感谢您的时间。
更新:
在下面添加了以下建议:
@QtCore.pyqtSlot()
def stop(self):
return
在我的SSITesterThread类中,当我在线程中使用的各种“发出”突然尝试访问NoneType对象时,它会出错:
File in gui.py, line 75 in tester,
self.updateColor.emit("{}".format(thread_colors.green_alert)
AttributeError: "NoneType" object has no attribute 'green alert'
修复是否有效,这是一个新问题?因为似乎事情仍然没有优雅地关闭。
答案 0 :(得分:1)
看起来您正在从QTDesigner生成的表单中导入GUI。试试这个:
self.ui.closeEvent = self.closeEvent
我相信你的问题是你在错误的地方编辑了错误的QMainWindow实例。
答案 1 :(得分:0)
您可能只需要将stop
方法装饰为插槽。
@QtCore.pyqtSlot()
def stop(self):
# do thread cleanup, stop thread