Waiting for a timer to terminate before continuing running the code

2024/10/11 15:19:37

The following code updates the text of a button every second after the START button was pressed. The intended functionality is for the code to 'wait' until the timer has stopped before continuing on with the execution of the code. That is, after START is pressed, the text of the second button is incremented to 3, and only then should the text I waited! appear on the console.


import sys
from PySide import QtGui, QtCoreclass Example(QtGui.QWidget):def __init__(self, parent=None):super(Example, self).__init__(parent)self.app_layout = QtGui.QVBoxLayout()self.setLayout(self.app_layout)self.setGeometry(300, 300, 50, 50)self.current_count = 0self.count_to = 4self.delay = 1000self.timer = QtCore.QTimer(self)self.timer.timeout.connect(self.updateButtonCount) # start buttonstart_button = QtGui.QPushButton()start_button.setText('START')start_button.clicked.connect(self.startCount)self.app_layout.addWidget(start_button)# number buttonself.number_button = QtGui.QPushButton()self.number_button.setText('0')self.app_layout.addWidget(self.number_button)def updateButtonCount(self):self.number_button.setText("%s" % self.current_count)self.current_count += 1if self.current_count == self.count_to:self.timer.stop()def startCount(self):self.current_count = 0self.timer.start(self.delay)# this loop hangs the GUI:while True:if not self.timer.isActive():breakprint 'I waited!'def main():app = QtGui.QApplication(sys.argv)example = Example()example.show()sys.exit(app.exec_())if __name__ == '__main__':main()

The above code hangs the GUI, and if I remove the while True: loop, the I waited! appears immidiately on the console.

I'm certain that the while True: loop is not the correct way to go about it, so I'm looking for suggestions.

Answer

The solution I found that works was to replace

while True:if not self.timer.isActive():break

with

while self.timer.isActive():QtGui.QApplication.processEvents()

I'm not certain that this is the best solution, though.

https://en.xdnf.cn/q/118310.html

Related Q&A

PySpark: how to resolve path of a resource file present inside the dependency zip file

I have a mapPartitions on an RDD and within each partition, a resource file has to be opened. This module that contains the method invoked by mapPartitions and the resource file is passed on to each ex…

Convert normal Python script to REST API

Here I have an excel to pdf conversion script. How can I modify it to act as a REST API? import os import comtypes.client SOURCE_DIR = D:/projects/python TARGET_DIR = D:/projects/python app = comtypes…

How to track changes in specific registry key or file with Python? [closed]

Closed. This question is seeking recommendations for software libraries, tutorials, tools, books, or other off-site resources. It does not meet Stack Overflow guidelines. It is not currently accepting …

How to use Android NDK to compile Numpy as .so?

Because the Numpy isnt a static library(it contains .py files, .pyc files, .so files, etc), so if I want to import it to my python code which is used in an Android phone(using CLE), I should recompile …

passing boolean function to if-condition in python

I"m learning python, and Im trying to do this, which I thought should be trivial, but apparently, its not. $python >>> def isTrue(data): ... "ping" in data ... >>>…

Unsupported operand type(s) for str and str. Python

Ive got the IF statement;if contactstring == "[Practice Address Not Available]" | contactstring == "[]":Im not sure what is going wrong(possibly the " "s?) but I keep ge…

getting Monday , june 5 , 2016 instead of June 5 ,2016 using DateTimeField

I have an app using Django an my my model has the following field: date = models.DateTimeField(auto_now_add=True,auto_now=False)Using that I get this: June 5, 2016, 9:16 p.m.but I need something like…

WeasyPrint usage with Python 3.x on Windows

I cant seem to get WeasyPrint to work on Windows with Python 3.4 or 3.5. Has anyone been able to do this? There arent forums at weasyprint.org and the IRC channel is dead. Ive been able to install …

matplotlib scatter array lengths are not same

i have 2 arrays like this x_test = [[ 14. 1.] [ 14. 2.] [ 14. 3.] [ 14. 4.] [ 14. 5.] [ 14. 6.] [ 14. 7.] [ 14. 8.] [ 14. 9.] [ 14. 10.] [ 14. 11.] [ 14. 12.]]y_test = [ 254.7 255…

APLpy/matplotlib: Coordinate grid alpha levels for EPS quality figure

In the normal matplotlib axes class, it is possible to set gridlines to have a certain transparency (alpha level). Im attempting to utilise this with the APLpy package using the following:fig = pyplot.…