在Python中是否有一种方法可以通过单击按钮来改变函数的工作方式



我用Python写了这个简单的程序,它由2个按钮(Start和Reset)以及00:00计时器的画布组成,它所做的是当你单击Start时,时间从(25分钟)开始倒计时,直到它达到00:00,并从"Work"更改label_text";Break"重新开始倒数(5分钟到00:00),将label_text更改为"Work"从"Break",一切工作完美,然而,我的问题是:如果我点击开始多次,两个倒计时工作在同一时间,我需要的是,当我点击开始,我不应该能够再次点击它,除非我点击重置按钮。

我可以使用["state"] = DISABLED按钮来修复它,但它没有帮助,如果您知道任何其他方法,我将非常感谢您的帮助,提前感谢您。

下面是我的代码:

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 = None
# ---------------------------- TIMER RESET ------------------------------- #
def reset_timer():
window.after_cancel(timer)
checkmark_label.config(text="")
canvas.itemconfig(time_text, text="00:00")
timer_label.config(text="Timer")
global reps
reps = 0

# ---------------------------- TIMER 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 == 0:
count_down(long_break_sec)
timer_label.config(text="Break", fg=RED)

elif reps % 2 == 0:
count_down(short_break_sec)
timer_label.config(text="Break", fg=PINK)
window.attributes('-topmost', 1)
else:
count_down(work_sec)
timer_label.config(text="Work", fg=GREEN)
window.attributes('-topmost', 0)
# ---------------------------- COUNTDOWN MECHANISM ------------------------------- #

def count_down(count):
count_min =  math.floor(count / 60)
count_sec = count % 60
if count_sec == 0:
count_sec = "00"
elif count_sec > 0 and count_sec < 10:
count_sec = f"0{count_sec}"
canvas.itemconfig(time_text, text=f"{count_min}:{count_sec}")
if count > 0:
global timer
timer = window.after(1000, count_down, count - 1)
else:
start_timer()
marks = ""
work_sessions = math.floor(reps/2)
for _ in range(work_sessions):
marks += "✔"
checkmark_label.config(text=marks)

# ---------------------------- UI SETUP ------------------------------- #
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)
time_text = canvas.create_text(103, 130, text="00:00", fill="white", font=(FONT_NAME, 35, "bold"))
canvas.grid(column=2, row=2)

# Labels
timer_label = Label(text="Timer",bg=YELLOW, fg=GREEN, font=(FONT_NAME, 50, "bold"))
timer_label.grid(column=2, row=1)
checkmark_label = Label(fg=GREEN)
checkmark_label.grid(column=2, row=4)

# Buttons
start_button = Button(text="Start", bg=GREEN, width=11, height=2, borderwidth=0, command=start_timer)
start_button.grid(column=1, row=3)
reset_button = Button(text="Reset", bg=GREEN, width=11, height=2, borderwidth=0, command=reset_timer)
reset_button.grid(column=3, row=3)

window.mainloop()

我不知道为什么你有这个麻烦,但你所需要的只是跟踪如果计时器已经计数或不。为此,我创建了一个名为is_count的全局变量。默认为False。当用户按下"开始"键时;按钮,则将此变量设置为True。如果用户再次按下此按钮,程序将看到计时器已经计数,它不会启动一个新的计时器。这个解决方案非常简单,但是很有效,而且效果很好。

应用补丁的代码:

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 = None
is_counting = False
# ---------------------------- TIMER RESET ------------------------------- #
def reset_timer():
global is_counting
is_counting = False
window.after_cancel(timer)
checkmark_label.config(text="")
canvas.itemconfig(time_text, text="00:00")
timer_label.config(text="Timer")
global reps
reps = 0

# ---------------------------- TIMER MECHANISM ------------------------------- #
def start_timer():
# Checking if timer already exists. If it is,
# this function won't do anything
global is_counting
if is_counting:
return
is_counting = True
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 == 0:
count_down(long_break_sec)
timer_label.config(text="Break", fg=RED)

elif reps % 2 == 0:
count_down(short_break_sec)
timer_label.config(text="Break", fg=PINK)
window.attributes('-topmost', 1)
else:
count_down(work_sec)
timer_label.config(text="Work", fg=GREEN)
window.attributes('-topmost', 0)
# ---------------------------- COUNTDOWN MECHANISM ------------------------------- #

def count_down(count):
count_min =  math.floor(count / 60)
count_sec = count % 60
if count_sec == 0:
count_sec = "00"
elif count_sec > 0 and count_sec < 10:
count_sec = f"0{count_sec}"
canvas.itemconfig(time_text, text=f"{count_min}:{count_sec}")
if count > 0:
global timer
timer = window.after(1000, count_down, count - 1)
else:
# Setting is_counting variable to False, because start_timer()
# won't work when timer is already counting
global is_counting
is_counting = False
start_timer()
marks = ""
work_sessions = math.floor(reps/2)
for _ in range(work_sessions):
marks += "✔"
checkmark_label.config(text=marks)

# ---------------------------- UI SETUP ------------------------------- #
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)
time_text = canvas.create_text(103, 130, text="00:00", fill="white", font=(FONT_NAME, 35, "bold"))
canvas.grid(column=2, row=2)

# Labels
timer_label = Label(text="Timer",bg=YELLOW, fg=GREEN, font=(FONT_NAME, 50, "bold"))
timer_label.grid(column=2, row=1)
checkmark_label = Label(fg=GREEN)
checkmark_label.grid(column=2, row=4)

# Buttons
start_button = Button(text="Start", bg=GREEN, width=11, height=2, borderwidth=0, command=start_timer)
start_button.grid(column=1, row=3)
reset_button = Button(text="Reset", bg=GREEN, width=11, height=2, borderwidth=0, command=reset_timer)
reset_button.grid(column=3, row=3)

window.mainloop()

最新更新