使用 tkinter .after() 方法理解阻塞“递归”的执行流程

问题描述 投票:0回答:1

我的问题在于理解为什么这个特定的代码片段能够成功执行,即使事先递归地调用了start_timer函数,根据我的理解,这应该会阻止这段代码的执行。

        marks = ""
        work_sessions = math.floor(reps/2)
        for _ in range(work_sessions):
            marks += "✔"
        check_marks.config(text=marks)

完整节目:

from tkinter import *
import math
# ---------------------------- CONSTANTS ------------------------------- #
PINK = "#e2979c"
RED = "#e7305b"
GREEN = "#9bdeac"
YELLOW = "#f7f5dd"
FONT_NAME = "Courier"
WORK_MIN = 25
SHORT_BREAK_MIN = 5
LONG_BREAK_MIN = 20
reps = 0

# ---------------------------- TIMER RESET ------------------------------- # 

# ---------------------------- TIMER MECHANISM ------------------------------- # 

# ---------------------------- COUNTDOWN MECHANISM ------------------------------- #

def start_timer():
    global reps
    reps += 1
    work_sec = WORK_MIN * 60
    short_break_sec = SHORT_BREAK_MIN * 60
    long_break_sec = LONG_BREAK_MIN * 60

    if reps == 8:
        timer_label.config(text="Long Break", fg=RED)
        count_down(long_break_sec)
    elif reps % 2 == 1:
        timer_label.config(text="Work", fg=GREEN)
        count_down(0)
    elif reps % 2 == 0:
        timer_label.config(text="Short Break", fg=PINK)
        count_down(short_break_sec)


# ---------------------------- UI SETUP ------------------------------- #

def count_down(count):
    global reps

    count_min = math.floor(count / 60)
    count_sec = count % 60

    if count_sec < 10:
        count_sec = f"0{count_sec}"

    canvas.itemconfig(timer_text, text=f"{count_min}:{count_sec}")
    if count >= 0:
        window.after(1000, count_down, count-1)
    else:
        start_timer()
        marks = ""
        work_sessions = math.floor(reps/2)
        for _ in range(work_sessions):
            marks += "✔"
        check_marks.config(text=marks)


window = Tk()
window.title("Pomodoro")
window.config(padx=100, pady=50, bg=YELLOW)

canvas = Canvas(width=200, height=224, bg=YELLOW, highlightthickness=0)
tomato_img = PhotoImage(file="tomato.png")
canvas.create_image(100, 112, image=tomato_img)
timer_text = canvas.create_text(100, 130, text="00:00", fill="white", font=(FONT_NAME, 28, "bold"))
canvas.grid(column=1, row=1)

timer_label = Label(text="Timer", fg=GREEN, font=(FONT_NAME, 34, "bold"))
timer_label.grid(column=1, row=0)

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

reset_button = Button(text="Reset")
reset_button.grid(column=2, row=2)

check_marks = Label(fg=GREEN, font=(FONT_NAME, 20, "bold"))
check_marks.grid(column=1, row=3)

window.mainloop()

我尝试过调试,但似乎在第一次调用时它被跳过了。

python tkinter recursion execution
1个回答
0
投票

您可能认为,由于

start_timer()
调用
count_down()
count_down()
然后最终再次调用
start_timer()
,因此
start_timer()
永远不会完成,因此下面的代码永远不应该执行。如果您使用某种循环而不是
window.after(1000, count_down, count-1)
,那就是真的。

但是

.after()
启动计划任务并且不等待它完成,因此一旦到达其中的第一个
start_timer()
.after()
就完成了。

您可以通过在

count_down()
中调试打印来看到这一点,如下所示:

...
if count >= 0:
    window.after(1000, count_down, count-1)
    print('after call')
else:
    print('start start')
    start_timer()
    print('start end')
...
© www.soinside.com 2019 - 2024. All rights reserved.