Reputation: 1486
i have got following:
class simpleapp_tk(tkinter.Tk):
def __init__(self,parent):
tkinter.Tk.__init__(self,parent)
self.parent = parent
self.initialize()
def clock(self): # timer tick
print("Tick")
def ButtonStartGraphClick(self): # button click
self.NewTimer.start()
def initialize(self): # constructor
self.NewTimer = Timer(1,self.clock)
if __name__ == "__main__":
app = simpleapp_tk(None)
app.geometry("500x250")
app.title("TSC")
app.mainloop()
But my timer always tick only once, if i click button once more i have got exception that thread is already running
Upvotes: 0
Views: 365
Reputation: 3265
Well, the documentation of Timer is actually not explicit about this, but Timer
will actually run only once after the interval is reached, by design. So you need to create some kind of repeating timer functionality by yourself.
The simplest solution here would be to just re-instantiate and start the timer with the call of clock
-function:
def clock(self):
print("Tick")
self.NewTimer = Timer(1, self.clock)
self.NewTimer.start()
You also cannot start a timer again when it is already running, so you would need to create some kind of prevention for that in the button click code, for example:
def __init__(self, parent):
...
self.timerRunning = False
self.initialize()
def ButtonSTartGraphClick(self):
if not self.timerRunning:
self.timerRunning = True
self.NewTimer.start()
Upvotes: 1