PyQt:窗口关闭但进程仍在运行

时间:2016-10-10 20:32:35

标签: python python-2.7 pyqt

只是一个简单的问题(不适合我):当我关闭窗口时,程序仍在运行。这是代码:

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()

为什么会发生这种情况,以及如何解决这个问题? (我试图研究谷歌,是的,有很多论坛有相同的问题,但我到目前为止没有运气来修复它。)

编辑:问题仍然没有解决,有人可以显示/告诉如何停止线程。程序退出时的时间?请:)

1 个答案:

答案 0 :(得分:1)

在窗口小部件(被覆盖的)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()

我已经测试过这个有效:

  • 没有线程,app退出;
  • 使用threading.Timer,但没有取消计时器,app永远不会退出;
  • 计时器取消,app退出
相关问题