私は、1つのguiスレッドと24 "テスタースレッドを持つPyQT GUIを持っています。彼らは正常に動作しますが、私はGUIを閉じるときにまだ起きているようです。 Pythonのクラッシュを避けるためにスレッドを正常に閉じるにはどうすればよいですか?GUI上のすべてのQThreadsを終了します。
#!/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_())
私は、このソリューションを実装しようとした:https://gist.githubusercontent.com/metalman/10721983/raw/15c6f115f9918fee7c1b88d0a549d4cc59a5b346/qapplication_about_to_quit_signal.py
をしかしエラーました:お時間を
attributeerror: 'function' object has no attribute '__pyqtSignature__'
感謝を。
UPDATE:
は、以下のように提案を追加しました:とき、さまざまなエラーを出し
私SSITesterThreadクラスで@QtCore.pyqtSlot()
def stop(self):
return
、それ私が突然アクセスしようとするスレッドにsingalsとして使用してきた「発します」なしタイプオブジェクト:
File in gui.py, line 75 in tester,
self.updateColor.emit("{}".format(thread_colors.green_alert)
AttributeError: "NoneType" object has no attribute 'green alert'
修正が機能しましたか?これは新しい問題ですか?物事はまだ優雅に閉鎖されていないように見えるので。
thread_colorとは何ですか?コードが何であるかわからなくても、ホエーが何かを診断するのは難しいです。 –
申し訳ありませんが、私は明確ではありませんでした。私のQThreadからの放射ですが、スレッドが起動しているときに通常はポピュレートされているNoneTypeオブジェクトを処理しようとしているので、GUI終了時に停止していないと思います。スレッドが閉じていることを確認できる方法はありますか?私はこれが私の推論だと認めています。 –
スレッドが終了するまでブロックするにはthread.wait()を使用できます。 –