i'm stucked on a Qtimer that don't start, so the number that i need to update on the GUI is never showed.
i don't want to use a while loop inside the code because i need to change some values in real time without any problem.
i really don't understand why it don't start... or better, it start, but don't run the function update_label.
any suggestion?
Thanks a lot for your time!
here is my code:
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.setObjectName("MainWindow")
self.resize(1300, 768)
self.setMinimumSize(1300,768)
_translate = QtCore.QCoreApplication.translate
self.setWindowTitle(_translate("Bot", "Bot"))
self.number = QtWidgets.QLabel(self)
self.number .setGeometry(QtCore.QRect(1100, 10, 300, 20))
self.number .setObjectName("number")
self.show()
self.threadpool = QThreadPool()
self.updater = Updater()
self.threadpool.start(self.updater)
self.updater.update_progress.latest_number.connect(self.update_number)
@pyqtSlot(str)
def update_number(self, val):
x = str(val)
self.number.setText("Current block: " + x)
class Updater(QRunnable):
def __init__(self):
super(Updater, self).__init__()
self.update_progress = WorkerSignal()
print("started")
def run(self):
self.timer = QTimer()
self.timer.setInterval(2000)
self.timer.timeout.connect(self.update_label)
self.timer.start()
def update_label(self):
provider = configfile.provider
number = str(nmbr.latest_numer(provider))
self.update_progress.latest_number.emit(number)
print("update label started")
After tyring to understand your script, I reached the conclusion that
QRunnableis automatically released (or stopped) onceUpdater.run(self)reaches its end.But this is supposed to happen, because that's why you should use a
QThreadPoolin the first place. It is supposed to recycle expired threads in the background of the application.So, as you're running a
QTimerin the thread,QThreadPoolthinks thatUpdaterthread is dead, while in reality, the timer is still running on the background of a background thread. So it does its job and releasesUpdaterfrom the Thread pool.What you must do in order to keep both the Timer and
Updateralive is to manager theUpdaterthread's lifecycle yourself.So instead of using
QThreadPoolandQRunnable, you must go one level lower in terms of abstraction, and use theQThreadandQObjectthemselves in order to control when the thread is going to be stopped.What you can do now is follow this pattern and start implementing from here. There are many tutorials that use the
moveToThreadmethod. Just be careful to not let the thread or any object be recycled by the python's garbage collector, in order to avoid many other problems.The script is working, however I use PySide2 instead of PyQt5. So some variable and modules names may change, so just rename them when you try to run the script on your end.
There were a few errors on
Worker.update_label(self), which is just a copied script from the oldUpdater.update_label(self). I don't know what are the variables:configfileornmbras they are not initialized on your post. So I made atry-exceptblock to handle the error and make the script work just for testing.