Reputation: 275
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?
Upvotes: 1
Views: 2737
Reputation: 50540
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.
Upvotes: 3