【发布时间】:2016-07-01 15:20:15
【问题描述】:
我写了一个简单的程序,它有 pyQt 界面和 2 个按钮(开始和取消)。开始按钮在后台运行一些计算(通过启动更新功能),并且由于线程,我仍然可以使用 UI。 但应用程序在 10 秒 - 2 分钟后崩溃。 UI 消失,程序关闭。
当我使用 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很陌生,我是第一次使用线程。
【问题讨论】:
标签: multithreading python-2.7 pyqt