2

I've been googeling all day, tried loads of different ways, but I can't get this code to work. I want a simple timer that run a function when you press "Start", and stops when you press "Stop".

Example: When you press start, the function will print every second "Hello World", until you press stop.

My code with some comments so you can understand faster:

import sys
from Tkinter import *
from threading import Timer
# a boolean for telling the timer to stop
stop_timer = False

mgui = Tk()

def work(var):
    # stop_timers sets to True or False with Start/Stop
    stop_timer = var
    # work_done will evaluate if timer should start or be canceled
    def work_done(var2):
        stop_timer = var2
        # if stop was pressed t.start() will be ignored
        if stop_timer == False:
            t.start()
        # if stop was pressed timer will stop
        if stop_timer == True:
            print "Stopped!"
            t.cancel()
    t = Timer(1, work, [False])
    print "Something"
    work_done(var)

mgui.geometry('450x450')
mgui.title('Test')

cmd1 = lambda: work(False)
btn = Button(mgui, text="Start", command =cmd1).place(x=50, y=50)
cmd2 = lambda: work(True)
btn2 = Button(mgui, text="Stop", command =cmd2).place(x=100, y=50)

mgui.mainloop()

As you can tell, I'm new to this shizzle! Thanks, mates!

4
  • Is there anything holy about tkinter? Or would you be very opposed to a solution in wxPython? Commented Jan 20, 2014 at 20:07
  • I am not a tkinter guy, but I guess I can help you out with PyQt. Also what is the problem with the code you have written? Commented Jan 20, 2014 at 20:08
  • 2
    Do you mean "timer" in the conventional sense, as in "a thing that counts the amount of time elapsed since starting"? Or "timer" in the threading sense, as in "An object representing an action that should be run only after a certain amount of time has passed"? Commented Jan 20, 2014 at 20:08
  • @wnnmaw Nothing holy about it at all! I looked around for a gui yesterday, and that came up. Please post your solution! gravetii Oh, I missed to say what was wrong! I'll edit that in the main post. But yeah, the timer never stops. Kevin in a threading way. It should run every second until I tell it to not do so. Commented Jan 20, 2014 at 20:17

2 Answers 2

2

This is a generic timer, you can also implement one in tkinter using after:

import time
import Tkinter as tk

import threading

class MyTimer(threading.Thread):

    def __init__(self, t):
        super(MyTimer,self).__init__()
        self.txt = t
        self.running = True

    def run(self):
        while self.running:
            self.txt['text'] = time.time()        


mgui = tk.Tk()
mgui.title('Test')

txt = tk.Label(mgui, text="time")
txt.grid(row=0,columnspan=2)

timer = None
def cmd1():
    global timer
    timer = MyTimer(txt)
    timer.start()
def cmd2():
    global timer
    if timer:
        timer.running = False
        timer= None

btn = tk.Button(mgui, text="Start", command =cmd1)
btn.grid(row=1,column=1)
btn2 = tk.Button(mgui, text="Stop", command =cmd2)
btn2.grid(row=1,column=2)

mgui.mainloop()
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the post, but I was not clear enough. What I'm looking for is a timer that loops a function.
What do you mean with loops a function?
a function to be called over and over again. I managed to get it working with your code - thanks a ton!
I was about to say you that inside MyTimer.run you can call the function you like, I've just updated the label as a way of showing it working ;)
0

By editing some in xndrme's post, I finally got it to work. Thank you!

I'll post the code here for possible future googlers.

import sys
from Tkinter import *
from threading import Timer

mgui = Tk()

def cmd2():
    global t
    if t:
        t.cancel()

def looper():
    global t
    t = Timer(1, looper)
    t.start()
    print "Hello World!"

mgui.geometry('450x450')
mgui.title('Test')

btn = Button(mgui, text="Start", command =looper).place(x=50, y=50)
btn2 = Button(mgui, text="Stop", command =cmd2).place(x=100, y=50)

mgui.mainloop()

Comments

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.