PyQt: Window is closed but process still running
<p>Just a simple problem (not for me): when I close the window, the program is still running. Here is the code:</p>

<pre><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 -&gt; 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()
</code></pre>

<p>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).</p>

<p>EDIT: problem is still not fixed, can someone show/tell how to stop threading.Time on program exit? Please :)</p>

<p>Call the timer's cancel() method in your widget's (overridden) closeEvent() method:</p>

<pre><code>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()
</code></pre>

<p>I've tested that this works: </p>

<ul>
<li>without the threading, app exits; </li>
<li>with the threading.Timer, but without the timer cancel, app never exits; </li>
<li>with the timer cancel, app exits</li>
</ul>

python python-2.7 pyqt