How can I kill a single shot QtCore.QTimer in PyQt4?

2024/10/2 14:22:15

So, in my application, I create a single QtCore.QTimer object and then call the singleShot method on it to evoke a function after say 60 secs. Now, at any given point in time, if I would need to call the singleShot method on it again and prevent the previous singleShot method from taking effect (that is preventing it from calling the caller passed to it, if the second time the singleShot is invoked before the first 60 seconds), what do I need to do? How can I 'kill' the previous QTimer and completely forget about it and work only with the current QTimer?

Can someone help me out with this?

Here's just a sample code:

def main():q = QtCore.QTimer()q.singleShot(4000, print_hello)q.killTimer(id)     ##how can I get the value of 'id' so that print_hello() is not called before the end of the 4 seconds?def print_hello():print 'hello'

Thanks

Answer

The problem is that QTimer.singleShot() does not return a reference to the QTimer. I don't know of anyway to get the Timer ID so you can kill it using that method. However, you can instantiate a normal QTimer and make it a single-shot timer (this is not what you have done in your code provided, calling singleShot on an instance of QTimer creates a new QTimer which you do not have access to.)

However, all is not lost. You can create a normal QTimer and convert it to a single shot timer using setSingleShot(True). This allows you to call the stop() method if you wish to abort the timer. See the code example below which does what you require, on a 3 second timeout. You can push the button as many times as you like in rapid succession, and it will print "hello" once, 3 seconds after you stop. If you push it once, wait 4 seconds, and push it again, it will of course print out twice!

Hope that helps!

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *class MyApp(QWidget):def __init__(self,*args,**kwargs):QWidget.__init__(self,*args,**kwargs)self.current_timer = Noneself.layout = QVBoxLayout(self)self.button = QPushButton('start timer')self.button.clicked.connect(self.start_timer)self.layout.addWidget(self.button)def start_timer(self):if self.current_timer:self.current_timer.stop()self.current_timer.deleteLater()self.current_timer = QTimer()self.current_timer.timeout.connect(self.print_hello)self.current_timer.setSingleShot(True)self.current_timer.start(3000)def print_hello(self):print 'hello'# Create QApplication and QWidget
qapp = QApplication(sys.argv)  
app = MyApp()
app.show()
qapp.exec_()
https://en.xdnf.cn/q/70837.html

Related Q&A

How to convert list of lists to a set in python so I can compare to other sets?

I have a list users_with_invites_ids_list, formed by loop where I append values to the list, in python that looks like this:...[ObjectId(55119e14bf2e4e010d8b48f2)], [ObjectId(54624128bf2e4e5e558b5a52)]…

How can I create an ODBC connection to SAS?

Im writing a program that needs to access SAS data. Ive downloaded the ODBC drivers for SAS and installed them, but I need to be able to create ODBC connections on the fly, programmatically. The foll…

How to extract links from a page using Beautiful soup

I have a HTML Page with multiple divs like:<div class="post-info-wrap"><h2 class="post-title"><a href="https://www.example.com/blog/111/this-is-1st-post/" t…

Verifying the integrity of PyPI Python packages

Recently there came some news about some Malicious Libraries that were uploaded into Python Package Index (PyPI), see:Malicious libraries on PyPI Malicious modules found into official Python repository…

How to get results from custom loss function in Keras?

I want to implement a custom loss function in Python and It should work like this pseudocode:aux = | Real - Prediction | / Prediction errors = [] if aux <= 0.1:errors.append(0) elif aux > 0.1 &am…

How to tell whether a file is executable on Windows in Python?

Im writing grepath utility that finds executables in %PATH% that match a pattern. I need to define whether given filename in the path is executable (emphasis is on command line scripts).Based on "…

Issue with python/pytz Converting from local timezone to UTC then back

I have a requirement to convert a date from a local time stamp to UTC then back to the local time stamp.Strangely, when converting back to the local from UTC python decides it is PDT instead of the or…

Regex to replace %variables%

Ive been yanking clumps of hair out for 30 minutes doing this one...I have a dictionary, like so:{search: replace,foo: bar}And a string like this:Foo bar %foo% % search %.Id like to replace each var…

Python kivy - how to reduce height of TextInput

I am using kivy to make a very simple gui for an application. Nothing complex, very simple layout.Nevertheless I am having a hard time with TextInputs...They always display with full height and I cant …

Python-Matplotlib boxplot. How to show percentiles 0,10,25,50,75,90 and 100?

I would like to plot an EPSgram (see below) using Python and Matplotlib. The boxplot function only plots quartiles (0, 25, 50, 75, 100). So, how can I add two more boxes?