Basic Tkinter countdown timer [duplicate]

*爱你&永不变心* 提交于 2019-11-30 09:28:09

问题


I am currently working on a project that requires are very simple countdown timer, that works in the tkinter GUI and that dosen't rely on a recursion. I have tried different things but nothing seems to work so far.

import time
from tkinter import *


root = Tk()
root.title("Timer")
root.geometry("100x100")

def countdown(count):
    label = Label(root, text= count)
    label.place(x=35, y=15)

for i in range(5,0,-1):
    countdown(i)
    time.sleep(1)

root.mainloop()

回答1:


You can't use sleep because it stops mainloop and program can't work. You can use root.after to call function after 1000ms (1s)

import tkinter as tk

def countdown(count):
    # change text in label        
    label['text'] = count

    if count > 0:
        # call countdown again after 1000ms (1s)
        root.after(1000, countdown, count-1)

root = tk.Tk()

label = tk.Label(root)
label.place(x=35, y=15)

# call countdown first time    
countdown(5)
# root.after(0, countdown, 5)

root.mainloop()



回答2:


Similar principle as furas's solution already posted, but using a StringVar:

import Tkinter

def button_countdown(i, label):
    if i > 0:
        i -= 1
        label.set(i)
        root.after(1000, lambda: button_countdown(i, label))
    else:
        close()

def close():
    root.destroy()

root = Tkinter.Tk()

counter = 10
button_label = Tkinter.StringVar()
button_label.set(counter)
Tkinter.Button(root, textvariable=button_label, command=close).pack()
button_countdown(counter, button_label)

root.mainloop()

This can be made more elegant if the pieces live in the same class (namely eliminate the need for the lambda), but I think you can get the point here.



来源:https://stackoverflow.com/questions/34029223/basic-tkinter-countdown-timer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!