0

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

2
  • It would make it a lot easier for people to help you if you post a minimal reproducible example. Commented Nov 4, 2016 at 10:18
  • i have edited it with all neccessary things i think Commented Nov 4, 2016 at 10:23

1 Answer 1

1

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()
Sign up to request clarification or add additional context in comments.

1 Comment

The button has variable name as Stop/Start so the prevention will not be a problem but thanks for the first comment i thought this Timer is working same like for example .Net Timer and has Ontick Event and repeating until stop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.