QThread Mystery Error - python

I'm currently attempting to create a threaded timer application using PyQt. Simple, right? I thought so too. However, after spending all day trying to figure out what's going wrong, I still have absolutely no idea. In all of my immense stubbornness, I have refused to give up on what was supposed to be a 15-minute project.
Heres mah codez:
__author__ = 'Darth Vader'
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMessageBox, QApplication, QDialog
from PyQt5.QtCore import QThread
from timerui import Ui_Form
import sys
import ctypes
import time
import threading
class Timer(QThread):
def makeNoise(self):
pass
def run(self):
self.ui.startButton.setStyleSheet('''QPushButton {color: red;font: bold 15px;}''')
self.ui.startButton.setEnabled(False)
self.hour = int(self.ui.spinBoxHrs.value())
self.min = int(self.ui.spinBoxMin.value())
self.sec = int(self.ui.spinBoxSec.value())
if self.sec:
self.countSec = self.sec
elif self.min and not self.sec:
self.countSec = 60
self.min -= 1
elif self.hour and not self.min and not self.sec:
self.min = 59
self.countSec = 60
print(self.countSec)
while self.countSec or self.hour or self.min:
if not self.countSec and self.min:
self.min -= 1
self.countSec = 60
elif not self.countSec and not self.min and self.hour:
self.hour -= 1
self.min = 59
self.sec = 60
elif not self.countSec and not self.min and not self.hour:
self.makeNoise()
break
time.sleep(1)
self.countSec -= 1
self.ui.startButton.setText("%dh %dm %ds" % (self.hour, self.min, self.sec))
self.ui.startButton.setEnabled(True)
self.ui.startButton.setText("Start")
self.ui.startButton.setStyleSheet('QPushButton{}')
def setup(self, gui):
self.ui = gui
def __init__(self):
QThread.__init__(self)
def start():
t = Timer()
t.start()
if __name__ == '__main__':
myappid = u'org.ayoung.timer'
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)
app = QApplication(sys.argv)
app.setWindowIcon(QtGui.QIcon('res/favicon.png'))
window = QDialog()
ui = Ui_Form()
ui.setupUi(window)
ui.startButton.clicked.connect(start)
window.show()
sys.exit(app.exec_())
And the error:
QThread: Destroyed while thread is still running
QMutex: destroying locked mutex
From what I've read, these two errors have something to do with garbage collection, but I have absolutely no idea how to fix them.
Thanks!

There are three issues with your code. You need to solve the first one before you can solve the third one (the second issue should go away once you solve the first issue)
You are confusing functions and methods. I can tell this because you have a function called start, but the fact that the first argument in the function signature is called self indicates that you want it to be a method of an object. You might want to read this for a good explanation of the difference between a function and a method.
As a consequence of the previous point, and the fact that the QPushButton.clicked signal emits a boolean, means that when start is called, the self variable contains True (rather than a reference to the instance of a class (otherwise known as an object) - which is what would happen is start was a method rather than a function)
The line self.t = Timer().start() does the following:
Timer() creates an instance of the Timer class
The start method of this instance is called
The return value of the start() method is stored in self.t (we're ignoring here the problem that self is not a reference to an object).
What you want to do is instead create the instance of Timer and assign it to self.t. Then call start() on self.t. For example:
self.t = Timer()
self.t.start()
This ensures that the Timer object is saved somewhere and does not get garbage collected.

def start(self):
self.t = Timer().start()
...
ui.startButton.clicked.connect(start)
Here you are connecting a signal to a slot. The parameter passed to the slot is the button state, which is a bool. So self is True in your case.

Related

How to display a timer using PyQt5 for Python

I'm making a game where I would like a timer to be displayed on the screen after the user clicks on 'NEW GAME', to keep track of how long they've been playing. I have a class that runs the timer fine by itself, but when I incorporate it into the rest of my game and then on top of that, try to display the updated values of the timer, no values in the UI are updated and the printout of the timer doesn't even occur in the terminal. I've tried running the timer in the same thread as the game-setup process and I've also tried creating a new thread to run the timer but neither work. The game loads up and functions fine, with the exception of the timer not counting upwards and not displaying the updated timer values. Where am I going wrong here?
Here is my standalone Timer class, which again, works fine by itself.
from PyQt5 import QtCore
import sys
def startThread(functionName, *args, **kwargs):
print(args)
if len(args) == 0:
t = threading.Thread(target=functionName)
else:
try:
t = threading.Thread(target=functionName, args=args, kwargs=kwargs)
except:
try:
if args is None:
t = threading.Thread(target=functionName, kwargs=kwargs)
except:
t = threading.Thread(target=functionName)
t.daemon = True
t.start()
class Timer(object):
def __init__(self):
super(Timer, self).__init__()
def start_timer(self):
print("Starting timer...")
Timer.timer = QtCore.QTimer()
Timer.time = QtCore.QTime(0, 0, 0)
Timer.timer.timeout.connect(self.tick)
Timer.timer.start(1000)
def tick(self):
Timer.time = Timer.time.addSecs(1)
self.update_UI('%s' % Timer.time.toString("hh:mm:ss"))
def update_UI(self, text_string):
print(text_string)
# This is where the text would be sent to try and update the UI
Timer().start_timer()
This is more or less how my game-setup class is structured - currently I'm showing the version that uses threading:
class BuildUI(PyQt5.QtWidgets.QMainWindow, Ui_game):
def __init__(self):
super(BuildUI, self).__init__()
self.setupUi(self)
self.easy_mode = 38
self.user_available_cells = []
self.start_easy_game.triggered.connect(lambda: self.setup_game(self.easy_mode))
def setup_game(self, hidden_count):
def create_game_board():
startThread(Timer().start_timer)
self.game = BuildGame()
#The BuildGame class is not deliberately not shown
startThread(create_game_board)
class GAME(object):
def __init__(self):
GAME.app = PyQt5.QtWidgets.QApplication(sys.argv)
GAME.UI = BuildUI()
GAME.UI.show()
GAME.app.exec_()
def main():
GAME()
if __name__ == '__main__':
main()
The key to getting this to work is by using signals. Leaving the Timer class exactly as it is, the only modifications to be done are in the initialization of the GAME class, a signal needs to be added at the beginning of the BuildUI class and then using emit() to trigger that signal just before the self.game = BuildGame() call.
class BuildUI(PyQt5.QtWidgets.QMainWindow, sudoku_ui.Ui_sudoku_game):
# This signal just triggers a msgbox to display, telling the user the game is loading
process_start = PyQt5.QtCore.pyqtSignal()
# this is called to automatically close the msgbox window
process_finished = PyQt5.QtCore.pyqtSignal()
# This signal, when called will start the timer
start_game_timer = PyQt5.QtCore.pyqtSignal()
def __init__(self):
super(BuildUI, self).__init__()
self.setupUi(self)
self.easy_mode = 38
self.user_available_cells = []
self.start_easy_game.triggered.connect(lambda: self.setup_game(self.easy_mode))
def setup_game(self, hidden_count):
def create_game_board():
self.game = BuildGame()
# Now that the game is built, the timer can start
# This is the emit which will start the timer
GAME.UI.start_game_timer.emit()
startThread(create_game_board)
class GAME(object):
def __init__(self):
GAME.app = PyQt5.QtWidgets.QApplication(sys.argv)
GAME.UI = BuildUI()
GAME.dialog_box = MsgPrompt()
# This is the key right here - initializing the timer class
GAME.timer = Timer()
# This line below attaches a function to the emit() call - which will kick off the timer
GAME.UI.start_game_timer.connect(GAME.timer.start_timer)
# The below referenced class is deliberately omitted from this post
GAME.UI.process_start.connect(GAME.dialog_box.show_dialog_box)
GAME.UI.process_finished.connect(GAME.dialog_box.hide_dialog_box)
GAME.UI.show()
GAME.app.exec_()
def main():
GAME()
if __name__ == '__main__':
main()

PyQt5 threading GUI does not work

I am trying to load some data which takes 30+ seconds. During this time I wish the user to see a small GUI which says "Loading .", then "Loading ..", then "Loading ...", then "Loading ." etc. I have done some reading and I think I have to put this in a separate thread. I found someone who had a similar problem suggesting the solution was this in the right spot:
t = threading.Thread(target=self.test)
t.daemon = True
t.start()
In a lower part of the file I have the test function
def test(self):
tmp = InfoMessage()
while True:
print(1)
and the InfoMessage function
from PyQt5 import uic, QtCore, QtGui, QtWidgets
import sys
class InfoMessage(QtWidgets.QDialog):
def __init__(self, msg='Loading ', parent=None):
try:
super(InfoMessage, self).__init__(parent)
uic.loadUi('ui files/InfoMessage.ui',self)
self.setWindowTitle(' ')
self.o_msg = msg
self.msg = msg
self.info_label.setText(msg)
self.val = 0
self.timer = QtCore.QTimer()
self.timer.setInterval(500)
self.timer.timeout.connect(self.update_message)
self.timer.start()
self.show()
except BaseException as e:
print(str(e))
def update_message(self):
self.val += 1
self.msg += '.'
if self.val < 20:
self.info_label.setText(self.msg)
else:
self.val = 0
self.msg = self.o_msg
QtWidgets.QApplication.processEvents()
def main():
app = QtWidgets.QApplication(sys.argv) # A new instance of QApplication
form = InfoMessage('Loading ') # We set the form to be our MainWindow (design)
app.exec_() # and execute the app
if __name__ == '__main__': # if we're running file directly and not importing it
main() # run the main function
When I run the InfoMessage function alone it works fine and it updates every 0.5 seconds etc. However, when I fun this as part of the loading file the GUI is blank and incorrectly displayed. I know it is staying in the test function because of the print statement in there.
Can someone point me in the right direction? I think I am missing a couple of steps.
First, there are two ways of doing this. One way is to use the Python builtin threading module. The other way is to use the QThread library which is much more integrated with PyQT. Normally, I would recommend using QThread to do threading in PyQt. But QThread is only needed when there is any interaction with PyQt.
Second, I've removed processEvents() from InfoMessage because it does not serve any purpose in your particular case.
Finally, setting your thread as daemon implies your thread will never stop. This is not the case for most functions.
import sys
import threading
import time
from PyQt5 import uic, QtCore, QtWidgets
from PyQt5.QtCore import QThread
def long_task(limit=None, callback=None):
"""
Any long running task that does not interact with the GUI.
For instance, external libraries, opening files etc..
"""
for i in range(limit):
time.sleep(1)
print(i)
if callback is not None:
callback.loading_stop()
class LongRunning(QThread):
"""
This class is not required if you're using the builtin
version of threading.
"""
def __init__(self, limit):
super().__init__()
self.limit = limit
def run(self):
"""This overrides a default run function."""
long_task(self.limit)
class InfoMessage(QtWidgets.QDialog):
def __init__(self, msg='Loading ', parent=None):
super(InfoMessage, self).__init__(parent)
uic.loadUi('loading.ui', self)
# Initialize Values
self.o_msg = msg
self.msg = msg
self.val = 0
self.info_label.setText(msg)
self.show()
self.timer = QtCore.QTimer()
self.timer.setInterval(500)
self.timer.timeout.connect(self.update_message)
self.timer.start()
def update_message(self):
self.val += 1
self.msg += '.'
if self.val < 20:
self.info_label.setText(self.msg)
else:
self.val = 0
self.msg = self.o_msg
def loading_stop(self):
self.timer.stop()
self.info_label.setText("Done")
class MainDialog(QtWidgets.QDialog):
def __init__(self, parent=None):
super(MainDialog, self).__init__(parent)
# QThread Version - Safe to use
self.my_thread = LongRunning(limit=10)
self.my_thread.start()
self.my_loader = InfoMessage('Loading ')
self.my_thread.finished.connect(self.my_loader.loading_stop)
# Builtin Threading - Blocking - Do not use
# self.my_thread = threading.Thread(
# target=long_task,
# kwargs={'limit': 10}
# )
# self.my_thread.start()
# self.my_loader = InfoMessage('Loading ')
# self.my_thread.join() # Code blocks here
# self.my_loader.loading_stop()
# Builtin Threading - Callback - Use with caution
# self.my_loader = InfoMessage('Loading ')
# self.my_thread = threading.Thread(
# target=long_task,
# kwargs={'limit': 10,
# 'callback': self.my_loader}
# )
# self.my_thread.start()
def main():
app = QtWidgets.QApplication(sys.argv)
dialog = MainDialog()
app.exec_()
if __name__ == '__main__':
main()
Feel free to ask any follow up questions regarding this code.
Good Luck.
Edit:
Updated to show how to run code on thread completion. Notice the new parameter added to long_task function.

Stopwatch (chronometre) doesn't work

I make a stopwatch (chronometre) in python using the library pyqt, but I have a problem when I press the reset button and then the start button, I see the time increase of two, and repeat the process I see that increase in threes and so on.
I do not understand why this happens. I hope some can help me.
This is the code:
# -*- coding: utf-8 -*-
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt4 import uic
class Cronometro(QWidget):
def __init__(self):
QWidget.__init__(self)
uic.loadUi("cronometro.ui", self)
self.sec = 0
self.timer = QTimer()
self.set_time()
# Conexion
self.btnStart.clicked.connect(self.start)
self.btnReset.clicked.connect(self.reset)
self.btnExit.clicked.connect(self.close)
def start(self):
self.timer.timeout.connect(self.counter)
self.timer.start(1000)
def reset(self):
self.timer.stop()
self.sec = 0
def counter(self):
self.sec += 1
self.set_time()
def is_timer_active(self):
return self.timer.isActive()
def set_time(self):
hora = self.sec / 3600
minutos = (self.sec % 3600) / 60
segundos = (self.sec % 3600) % 60
self.label.setText("%02d:%02d:%02d" % (hora, minutos, segundos))
app = QApplication(sys.argv)
ventana = Cronometro()
ventana.show()
sys.exit(app.exec_())
Thank you very much !!!
The problem is here, in your start method:
def start(self):
self.timer.timeout.connect(self.counter)
self.timer.start(1000)
Every time you start the timer, you connect the timer's timeout signal to your counter method. So if you start the timer twice, there are two connections from the timer to your counter method, so the counter method gets called twice on every tick of the timer.
The fix is fairly straightforward: move the line
self.timer.timeout.connect(self.counter)
into your __init__ method, so that the connection is only ever made once.

Update QWidget every minutes

Is their a way possible to update a Qwidget in PyQT4 every 15 minutes ? I know their is something like a Qtimer but is it also possible to make a QWidget update itself at a specific time,for example 00h00 00h15 00h30 00h45 01h00,... . So is their a way to make the Qtimer depend on the time?
The QTimer class has a setInterval method. You can utilize this to change the wait time on the fly. As a short example, this block of code will show the current second. However, if the second is a multiple of 10 it will wait 5 seconds before starting again:
import sys
from PyQt4 import QtGui, QtCore
from time import strftime
class Main(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.initUI()
def initUI(self):
self.timer = QtCore.QTimer(self)
self.timer.timeout.connect(self.Time)
self.timer.start(1000)
self.lcd = QtGui.QLCDNumber(self)
self.lcd.display(strftime("%S"))
self.setCentralWidget(self.lcd)
self.setGeometry(300,300,250,100)
def Time(self):
if int(strftime("%S")) % 10 == 0:
self.timer.setInterval(5000)
else:
self.timer.setInterval(1000)
self.lcd.display(strftime("%S"))
def main():
app = QtGui.QApplication(sys.argv)
main = Main()
main.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
(This was modified slightly from a tutorial showing how to build a digital clock)
It looks like this for 4 consecutive changes of the display:
For your particular problem, when you start the application, you can find how long it is until the next quarter hour via a function like is presented in this answer:
def next_quarter_hour(self):
dt = datetime.datetime.now()
nsecs = dt.minute*60+dt.second+dt.microsecond*1e-6
delta = (nsecs//900)*900+900-nsecs
return delta * 1000.0
This would change the following:
def initUI(self):
...
self.timer.start(next_qt_hour)
...
def Time(self):
if int(strftime("%M")) % 15 == 0:
self.timer.setInterval(900000)
self.lcd.display(strftime("%M"))
Use this to set your first interval. Then once that timer has been exhausted, reset your interval to 15 minutes.

Python: How to Link Multi-Processes to Multi-Progress Bars

It seems I've got dangerously close to being able to link all the sub-processes initiated by subprocessing's Pool() to PyQt Progress Bar widgets. Here is the example code. Apparently there are few flows that stop my dream from happening.
The concept:
externalFunc() performs all the main tasks. From time to time it sends its 'progress values' to MainWindow() instance by storing its VALUE into poolDict variable declared with:
poolDict=manager.dict()
Meanwhile myEvenListener() method is running at the background awaiting for VALUE to be 'posted'. As soon as VALUE "received" myEvenListener() updates a ProgressBar to a received VALUE and resets it back to zero (to avoid the duplicate progressBar updates).
myEvenListener(self) before trying to update ProgressBar verifies the ProgressBar didn't yet reach its 100 maximum value before proceeding by using:
if pb.value()>=100: continue
Unfortunately even while externalFunc() supplies with more than enough values (160) the ProgressBar never reaches 100. Aside from it there is no way to exit MainWindow() while loop.
Please be carefull running this code before you read it since it may generate multi-python-processes on your machine that will have to be terminated.
Question:
Why does ProgressBar never reach its maximum 100
How to make sure myEvenListener() method is not running when it is not needed.
How to make sure all the sub-processes die after:
a. all the progressBars are at their 100%
b. user closes dialog box or terminates main process.
import sys, time
from PyQt4 import QtCore, QtGui
import multiprocessing as mp
from multiprocessing import Pool
manager = mp.Manager()
poolDict=manager.dict()
class PbWidget(QtGui.QProgressBar):
def __init__(self, parent=None, total=20):
super(PbWidget, self).__init__()
self.setMinimum(1)
self.setMaximum(total)
self._active = False
def update_bar(self, to_add_number):
while True:
time.sleep(0.01)
value = self.value() + to_add_number
self.setValue(value)
QtGui.qApp.processEvents()
if (not self._active or value >= self.maximum()):
break
self._active = False
def closeEvent(self, event):
self._active = False
def externalFunc(each):
for i in range(16):
print i
poolDict[each]=10+i
time.sleep(0.5)
class MainWindow(QtGui.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
# self.myList=[.1, .3, .5, 1.0, 1.5, 2.9, 3.1]
self.myList=[1]
self.main_layout = QtGui.QVBoxLayout()
self.pBars={}
self.state=True
for each in self.myList:
pb=PbWidget(total=101)
self.main_layout.addWidget(pb)
self.pBars[each]={'pb':pb, 'value':0, 'total_value':0}
ok_button = QtGui.QPushButton("OK")
ok_button.clicked.connect(self.OK)
self.main_layout.addWidget(ok_button)
central_widget = QtGui.QWidget()
central_widget.setLayout(self.main_layout)
self.setCentralWidget(central_widget)
def myEvenListener(self):
"""This function runs at the background as an infinite while loop. It constantly reads
a value stored in poolDict variable to which externalFunc() writes a new value every second.
The value represents a 'Progress' and needs to be passes to Progress Bar widget to which only MainWindow()
class has an access. After a Value was read it is used to update a Progress Bar.
After a progress bar was updated the Value is reset to zero.
The Problem: 'if pb.value()>=100' statement is used to make sure the function doesn't loop if the ProgressBar
is already reached 100. By some reason the bar never reaches its maximum 100 even while externalFunc() calls
enough times to reach this number. An ussue # 2: There is no meachanism to exit this while loop without adding
three more variables... Can it be achieved with what we already have?
"""
while self.state:
for each in self.pBars:
pb = self.pBars[each]['pb']
print "\n Current Bar value =", pb.value()
if pb.value()>=100:
print 'skipping'
continue
value=None
if each in poolDict.keys():
# read delivered value
delivered_value=poolDict[each]
# reset delivered value
poolDict[each]=0
# update bar with delivered value
if ( 101-pb.value() ) < delivered_value:
print "\n\t UPDATING WITH "
pb.update_bar( 101-pb.value() )
print "\n\t AFTER ", pb.value()
else:
pb.update_bar( delivered_value )
# print '\n\t\t\t Updating bar using this value', delivered_value
def OK(self):
pool = Pool(processes=3)
pool.map_async( externalFunc, self.myList)
self.myEvenListener()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = MainWindow()
window.resize(480, 320)
window.show()
sys.exit(app.exec_())
Here is a revised code. It seems to be working well and it is quite stable.
import sys, time
from PyQt4 import QtCore, QtGui
import multiprocessing as mp
from multiprocessing import Pool
manager = mp.Manager()
poolDict=manager.dict()
class PbWidget(QtGui.QProgressBar):
def __init__(self, parent=None, total=20):
super(PbWidget, self).__init__()
self.setMinimum(1)
self.setMaximum(total)
self._active = False
def update_bar(self, to_add_number):
while True:
time.sleep(0.01)
value = self.value() + to_add_number
self.setValue(value)
QtGui.qApp.processEvents()
if (not self._active or value >= self.maximum()):
break
self._active = False
def closeEvent(self, event):
self._active = False
def externalFunc(each):
for i in range(16):
value =10+i
poolDict[each]=value
time.sleep(each)
class MainWindow(QtGui.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.myList=[.5, .8, 1.2, 1.8, 2.2, .3, .1]
self.main_layout = QtGui.QVBoxLayout()
self.pBars={}
self.state=True
self.name=None
for each in self.myList:
pb=PbWidget(total=101)
self.main_layout.addWidget(pb)
self.pBars[each]={'pb':pb, 'name':each}
ok_button = QtGui.QPushButton("Distribute")
ok_button.clicked.connect(self.OK)
self.main_layout.addWidget(ok_button)
central_widget = QtGui.QWidget()
central_widget.setLayout(self.main_layout)
self.setCentralWidget(central_widget)
def myEvenListener(self):
"""This function runs at the background as an infinite loop. It is constantly reading
a value stored in poolDict variable to which externalFunc() writes a new value.
The value represents a 'Progress' and needs to be passed to Progress Bar widget to which MainWindow()
class has an access. After a Value was read and used to update a Progress Bar it is reset to zero.
"""
entities = self.pBars.keys()
while self.state:
for each in entities:
if each not in self.pBars.keys(): continue
pb = self.pBars[each]['pb']
if pb.value()>=100:
self.pBars.pop(each, None)
value=None
if each in poolDict.keys():
# read delivered value
delivered_value=poolDict[each]
# reset delivered value
poolDict[each]=0
# update bar with delivered value
if ( 101-pb.value() ) < delivered_value:
pb.update_bar( 101-pb.value() )
elif delivered_value>0:
pb.update_bar( delivered_value )
if len(self.pBars.keys())==0:
self.state=False
def OK(self):
pool = Pool(processes=3)
pool.map_async( externalFunc, self.myList)
self.myEvenListener()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = MainWindow()
window.resize(480, 320)
window.show()
sys.exit(app.exec_())

Categories