我寫了一個簡單的程序,它有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