我一直在研究 python tkinter 番茄鐘。除了恢復按鈕之外,其他所有東西都可以完美運行。這是我的代碼:
from tkinter import *
window = Tk()
# ---------------------------- CONSTANTS ------------------------------- #
PINK = "#e2979c"
RED = "#e7305b"
GREEN = "#9bdeac"
YELLOW = "#f7f5dd"
FONT_NAME = "Courier"
rep = 0
timer_var = None
count_min = 0
count_sec = 0
#----------------------------Pause----------------------------------------#
def pause():
global timer_var
if pause_button['text'] == 'pause':
window.after_cancel(timer_var)
pause_button.config(text='resume')
if pause_button['text'] == 'resume':
countdown(count_min * 60 count_sec)
timer_function()
# ---------------------------- TIMER RESET ------------------------------- #
def reset_timer():
global timer_var
global rep
window.after_cancel(timer_var)
canvas.itemconfig(countdown_text, text=f"")
timer_label.config(text='Timer', fg=GREEN)
marks = ''
checkmark.config(text=marks)
rep = 0
start_button.config(state='active')
# ---------------------------- TIMER MECHANISM ------------------------------- #
def timer_function():
global rep
if int(count_min) * 60 int(count_sec) == 0:
rep = 1
start_button.config(state='disabled')
if rep in (1, 3, 5, 7): # instead of saying rep == smth or rep ==smth2 use this
timer_label.config(text='work', fg=GREEN)
countdown(20)
elif rep in (2, 4, 6):
timer_label.config(text='short break', fg=RED)
countdown(5)
elif rep == 8:
timer_label.config(text='long break', gf=RED)
countdown(10)
elif rep == 9:
rep = 0
# ---------------------------- COUNTDOWN MECHANISM ------------------------------- #
def countdown(count):
global count_min
global count_sec
count_min = count // 60
count_sec = count - count_min * 60
if count_sec < 10:
count_sec = '0' str(count_sec)
canvas.itemconfig(countdown_text, text=f"{count_min}:{count_sec}")
if count > 0:
global timer_var
timer_var = window.after(1000, countdown, count -
1) # this part is recursion
else:
timer_function() # cant use loop so have to call the function again and again
work_session_num = rep // 2
marks = ''
for _ in range(work_session_num):
marks = '?'
checkmark.config(text=marks)
# ---------------------------- UI SETUP ------------------------------- #
window.title('pomodoro')
window.config(padx=90, pady=60, bg=YELLOW)
file_location = PhotoImage(file='day28/tomato.png')
timer_label = Label(text='Timer', width=12, font=(
FONT_NAME, 60, 'normal'), fg=GREEN, bg=YELLOW)
timer_label.grid(row=0, column=1)
start_button = Button(text='start', command=timer_function, state='active')
start_button.grid(row=2, column=0)
reset_button = Button(text='reset', command=reset_timer)
reset_button.grid(row=2, column=2)
pause_button = Button(text='pause', command=pause)
pause_button.grid(row=2, column=1)
checkmark = Label(bg=YELLOW, fg=GREEN, font=(FONT_NAME, 15, 'bold'))
checkmark.grid(row=3, column=1)
canvas = Canvas(width=200, height=224, bg=YELLOW, highlightthickness=0)
canvas.create_image(100, 112, image=file_location)
canvas.grid(row=1, column=1)
countdown_text = canvas.create_text(100, 130, fill='white',
font=(FONT_NAME, 35, 'bold'))
window.mainloop()
“暫停功能”有一些問題。我可以讓秒表暫停,但是當恢復時,秒表開始隨機增加和減少。幫助將不勝感激。
uj5u.com熱心網友回復:
我嘗試運行您的代碼并添加了一些列印陳述句。似乎發生的是,當您“暫停”或“繼續”時,倒計時實際上并未停止。起初,您的代碼countdown()每秒都會呼叫一次。然后當我暫停計數器時,它每秒被呼叫兩次。您的倒計時功能實際上并未取消。
如果您仔細查看再次點擊“恢復”時看到的“隨機”數字,您會發現它實際上并不是隨機的。您會看到兩個計數器同時倒計時。例如,您將看到18, 10, 17, 9, 16, 8.
Counter 1: 18, 17, 16和Counter 2: 10, 9, 8。
uj5u.com熱心網友回復:
我不知道暫停對你來說是如何作業的,因為你使用了 if 而不是 elif,所以當它變成“恢復”時,它會繼續運行。
另外,我不建議你直接使用按鈕上的文字來宣告時鐘是暫停還是運行,而是使用另一個變數。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/393611.html
