Marius Valentavicius Marius Valentavicius - 1 year ago 88
Python Question

PyQt: Window is closed but process still running

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 = '0'

super(MainWindow, self).__init__()
self.setGeometry(50, 50, 250, 150)
self.setWindowTitle("Title here")
self.setFixedSize(250, 150)


#make widgets (progressBar and labe)
def home(self):
self.prgB = QtGui.QProgressBar(self)
self.prgB.setGeometry(20, 20, 210, 20)
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()


#update() to update label and progressbar values
def update(self):
textas = ('%s : %.1f' % (, self.tempValue))
self.lbl.setText(str(textas + ' C'))
self.prgB.setFormat(QString.number(self.tempValue)+ ' C')

#temp() to get chip data from sensors (temp, name etc)
def tmp(self):
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() = feature.label
#print ('%s (%r): %.1f' % (, feature.label, feature.get_value()))

threading.Timer(2.0, self.tmp).start()

self.signalUpdate.emit() #emit signal

def run():
app = QtGui.QApplication(sys.argv)
GUI = MainWindow()


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

Answer Source

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.signalUpdate.emit()    #emit signal

def closeEvent(self):

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download