PyQt:窗口关闭但进程仍在运行(PyQt: Window is closed but process still running)
只是一个简单的问题(不适合我):当我关闭窗口时,程序仍在运行。 这是代码:
from PyQt4 import QtCore, QtGui from PyQt4.Qt import QString import sys import sensors from sensors import * import threading class MainWindow(QtGui.QWidget): signalUpdate = QtCore.pyqtSignal() # 1 - define a new signal in mainwindow class # 2 -connect this signal to the update() function #emit signal #main window def __init__(self): #vars for core temp and name self.tempValue = 0 self.name = '0' super(MainWindow, self).__init__() self.setGeometry(50, 50, 250, 150) self.setWindowTitle("Title here") self.setFixedSize(250, 150) self.home() #make widgets (progressBar and labe) def home(self): self.prgB = QtGui.QProgressBar(self) self.prgB.setGeometry(20, 20, 210, 20) #self.prgB.setOrientation(QtCore.Qt.Vertical) QtGui.QApplication.setStyle(QtGui.QStyleFactory.create("motif"))#stles -> motif, Plastique, Windows self.lbl = QtGui.QLabel(self) self.lbl.setGeometry(60, 40, 210, 20) self.signalUpdate.connect(self.update) #connect this signal to the update() function lay = QtGui.QVBoxLayout() lay.addWidget(self.prgB) lay.addWidget(self.lbl) self.setLayout(lay) self.tmp() self.show() #update() to update label and progressbar values def update(self): textas = ('%s : %.1f' % (self.name, self.tempValue)) self.lbl.setText(str(textas + ' C')) self.prgB.setFormat(QString.number(self.tempValue)+ ' C') self.prgB.setValue(self.tempValue) #temp() to get chip data from sensors (temp, name etc) def tmp(self): sensors.init() try: for chip in sensors.iter_detected_chips(): #print (chip) #print('Adapter:', chip.adapter_name) for feature in chip: if feature.label == 'Physical id 0': self.tempValue = feature.get_value() self.name = feature.label #print ('%s (%r): %.1f' % (feature.name, feature.label, feature.get_value())) threading.Timer(2.0, self.tmp).start() self.signalUpdate.emit() #emit signal #print finally: sensors.cleanup() def run(): QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_X11InitThreads) app = QtGui.QApplication(sys.argv) GUI = MainWindow() sys.exit(app.exec_()) run()为什么会发生这种情况,以及如何解决这个问题? (我试图研究谷歌,是的,有很多论坛有相同的问题,但我到目前为止没有运气来解决它)。
编辑:问题仍然没有修复,有人可以显示/告诉如何停止threading.Time程序退出? 请 :)
Just a simple problem (not for me): when I close the window, the program is still running. Here is the code:
from PyQt4 import QtCore, QtGui from PyQt4.Qt import QString import sys import sensors from sensors import * import threading class MainWindow(QtGui.QWidget): signalUpdate = QtCore.pyqtSignal() # 1 - define a new signal in mainwindow class # 2 -connect this signal to the update() function #emit signal #main window def __init__(self): #vars for core temp and name self.tempValue = 0 self.name = '0' super(MainWindow, self).__init__() self.setGeometry(50, 50, 250, 150) self.setWindowTitle("Title here") self.setFixedSize(250, 150) self.home() #make widgets (progressBar and labe) def home(self): self.prgB = QtGui.QProgressBar(self) self.prgB.setGeometry(20, 20, 210, 20) #self.prgB.setOrientation(QtCore.Qt.Vertical) QtGui.QApplication.setStyle(QtGui.QStyleFactory.create("motif"))#stles -> motif, Plastique, Windows self.lbl = QtGui.QLabel(self) self.lbl.setGeometry(60, 40, 210, 20) self.signalUpdate.connect(self.update) #connect this signal to the update() function lay = QtGui.QVBoxLayout() lay.addWidget(self.prgB) lay.addWidget(self.lbl) self.setLayout(lay) self.tmp() self.show() #update() to update label and progressbar values def update(self): textas = ('%s : %.1f' % (self.name, self.tempValue)) self.lbl.setText(str(textas + ' C')) self.prgB.setFormat(QString.number(self.tempValue)+ ' C') self.prgB.setValue(self.tempValue) #temp() to get chip data from sensors (temp, name etc) def tmp(self): sensors.init() try: for chip in sensors.iter_detected_chips(): #print (chip) #print('Adapter:', chip.adapter_name) for feature in chip: if feature.label == 'Physical id 0': self.tempValue = feature.get_value() self.name = feature.label #print ('%s (%r): %.1f' % (feature.name, feature.label, feature.get_value())) threading.Timer(2.0, self.tmp).start() self.signalUpdate.emit() #emit signal #print finally: sensors.cleanup() def run(): QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_X11InitThreads) app = QtGui.QApplication(sys.argv) GUI = MainWindow() sys.exit(app.exec_()) run()Why is that happening, and how to fix it? (I was trying to research on google and yes there are many forums with same question but I get no luck so far to fix it).
EDIT: problem is still not fixed, can someone show/tell how to stop threading.Time on program exit? Please :)
最满意答案
在窗口小部件(覆盖)的closeEvent()方法中调用计时器的cancel()方法:
def tmp(self): ... self.timer = threading.Timer(2.0, self.tmp) self.timer.start() self.signalUpdate.emit() #emit signal def closeEvent(self): self.timer.cancel()我测试过这个有效:
没有线程,应用程序退出; 使用threading.Timer,但没有取消计时器,应用程序永远不会退出; 随着计时器取消,应用程序退出Call the timer's cancel() method in your widget's (overridden) closeEvent() method:
def tmp(self): ... self.timer = threading.Timer(2.0, self.tmp) self.timer.start() self.signalUpdate.emit() #emit signal def closeEvent(self): self.timer.cancel()I've tested that this works:
without the threading, app exits; with the threading.Timer, but without the timer cancel, app never exits; with the timer cancel, app exits更多推荐
发布评论