我写了一个简单的程序,它有2个按钮(启动和取消)的pyQt接口。开始按钮在后台运行一些计算(通过启动更新功能),并且由于线程,我仍然可以使用UI。 但是应用程序在10秒-2分钟后崩溃。 UI只是消失,程序关闭。pyQt和线程应用程序崩溃
当我使用pythonw无需控制台线程崩溃运行的应用程序〜25秒,但GUI后仍然有效。
#!/usr/bin/python
import threading
import sys
from PyQt4 import QtGui, QtCore
import time
import os
class Class(QtGui.QWidget):
def __init__(self):
#Some init variables
self.initUI()
def initUI(self):
#some UI
self.show()
def update(self,stop_event):
while True and not stop_event.isSet():
self.updateSpeed()
self.updateDistance()
self.printLogs()
self.saveCSV()
self.guiUpdate()
time.sleep(1)
#gui button function
def initiate(self):
self.stop_event = threading.Event()
self.c_thread = threading.Thread(target = self.update, args=(self.stop_event,))
self.c_thread.start()
#Also gui button function
def cancelTracking(self):
self.stop_event.set()
self.close()
def main():
app = QtGui.QApplication(sys.argv)
ex = Class()
sys.exit(app.exec_())
ex.update()
if __name__ == '__main__':
main()
我不知道我是否在线程正确。我在堆栈中找到了这样的例子。我对Python非常陌生,我第一次使用线程。
谢谢!它的工作原理就像它应该;)没有更多的崩溃 – Karmel