python-3.x 如何创建一个倒计时定时器,在按下一次按钮后继续计数,更新tkinter画布中的文本

j9per5c4  于 2022-11-26  发布在  Python
关注(0)|答案(2)|浏览(129)

我正在创建一个tkinter窗口,其中有一个倒计时器和一个按钮。一旦我按下按钮,我希望计时器连续倒计时。窗口中的文本也应该每秒倒计时一次,直到到达0:00。
到目前为止,我已经使用了这段代码。我试过time.sleep()和window.after()来尝试计时,但是我的tkinter窗口要么在我按下按钮之前显示2:00,要么在一秒钟之后显示0:00。

def countdown():
    total_seconds = 60
    total_minutes = 2

    while total_seconds != 0:
        if total_seconds == 60:
            total_seconds -= 1
            total_minutes -= 1
            time.sleep(1)
            canvas.itemconfig(timer_text, text=f"{total_minutes}:{total_seconds}")
        elif total_seconds == 1 and total_minutes != 0:
            total_seconds += 59
            time.sleep(1)
            canvas.itemconfig(timer_text, text=f"{total_minutes}:00")
        elif total_seconds == 0 and total_minutes > 0:
            total_seconds = 59
            total_minutes -= 1
            time.sleep(1)
            canvas.itemconfig(timer_text, text=f"{total_minutes}:{total_seconds}")
        elif total_seconds < 10:
            total_seconds -= 1
            time.sleep(1)
            canvas.itemconfig(timer_text, text=f"{total_minutes}:0{total_seconds}")
        else:
            total_seconds -= 1
            time.sleep(1)
            canvas.itemconfig(timer_text, text=f"{total_minutes}:{total_seconds}")

window = Tk()
window.title("Title comes here")
window.config(padx=100, pady=50, bg=BLUE)


canvas = Canvas(width=400, height=450, bg=BLUE, highlightthickness=0)
timer_text = canvas.create_text(210, 100, text="2:00", fill="white", font=(FONT_NAME, 35, "bold"))
canvas.grid(column=1, row=1)

start_button = Button(text="Start", command=countdown)
start_button.grid(column=0, row=2)
3mpgtkmj

3mpgtkmj1#

这是一个没有while循环的解决方案,并按照建议实现了after。您更新时间的条件已经很好了。

import tkinter as tk
import time

def countdown(total_seconds=60, total_minutes=2):
    if total_seconds == 60:
        total_seconds -= 1
        total_minutes -= 1
        timer_text.config(text=f"{total_minutes}:{total_seconds}")
        window.after(1000, countdown, total_seconds, total_minutes)        
    elif total_seconds == 1 and total_minutes != 0:
        total_seconds += 59
        timer_text.config(text=f"{total_minutes}:00")
        window.after(1000, countdown, total_seconds, total_minutes)        
    elif total_seconds == 0 and total_minutes > 0:
        total_seconds = 59
        total_minutes -= 1
        timer_text.config(text=f"{total_minutes}:{total_seconds}")
        window.after(1000, countdown, total_seconds, total_minutes)        
    elif total_seconds < 11 and total_seconds > 0:
        total_seconds -= 1
        timer_text.config(text=f"{total_minutes}:0{total_seconds}")
        window.after(1000, countdown, total_seconds, total_minutes)   
    elif total_seconds == 0 and total_minutes == 0:
        pass
    else:
        total_seconds -= 1
        timer_text.config(text=f"{total_minutes}:{total_seconds}")
        window.after(1000, countdown, total_seconds, total_minutes)        

window = tk.Tk()
window.title("Title comes here")
window.config(padx=100, pady=50, bg='BLUE')

timer_text = tk.Label(window, text="TIMER", font=('Arial', 35, "bold"), bg='blue')
timer_text.grid(column=1, row=1)

start_button = tk.Button(text="Start", command=countdown)
start_button.grid(column=0, row=2)

window.mainloop()
ajsxfq5m

ajsxfq5m2#

不建议在tkinter应用程序中使用while循环,请改用.after()
下面是使用.after()修改的countdown()

def countdown(seconds=120):
    mins, secs = divmod(seconds, 60)
    canvas.itemconfig(timer_text, text=f"{mins}:{secs:02}")
    if seconds > 0:
        canvas.after(1000, countdown, seconds-1)

相关问题