[Tutor] Countdown Clock Programming Question

Alan Gauld alan.gauld at btinternet.com
Tue Dec 15 12:31:06 EST 2015


On 15/12/15 17:10, Alan Gauld wrote:

> ############################
> import tkinter as tk
> 
> def count_down(start,stop):
>    display['text'] = str(start)
>    if start > stop:
>         top.after(1000, lambda : count_down(start-1,stop))
> 
> top = tk.Tk()
> display = tk.Button(top,text="Press",
>                     command = lambda: count_down(10,0))
> display.pack()
> top.mainloop()
> ############################

In case lambda confuses things, here is a version
using globals and no lambdas:

############################
import tkinter as tk

start = 10
stop = 0

def count_down():
    global start
    display['text'] = str(start)
    if start > stop:
        start -= 1
        top.after(1000, count_down)

top = tk.Tk()
display = tk.Button(top,text="Press",command = count_down)
display.pack()
top.mainloop()
############################


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list