所以我正在 tkinter 中开发一个简单的 GUI 秒表项目,其中包含一个暂停按钮(用于暂停时间)、一个播放按钮(用于再次计时)和一个重置按钮(用于将秒表时间设置回 00:00) 。我还添加了一个精美的 GIF 背景(您可以忽略)并编写了一堆代码,以便将其渲染为实际的动画 GIF(您也可以忽略)。我在这个背景上添加了一个标签,该标签应该显示我也使用了这个秒表模块:(https://pypi.org/project/stopwatch.py/)
import tkinter as tk
from PIL import Image
from stopwatch import Stopwatch
stopwatch_running = False
hours = "0"
minutes = "0"
seconds = "0"
stopwatch = Stopwatch(2)
stopwatch.stop()
def pause_time():
pass
def play_time():
global stopwatch_running, hours, minutes, seconds
if not stopwatch_running:
stopwatch.start()
stopwatch_running = True
time = str(stopwatch)
if len(time) > 5:
seconds = "00"
minutes = f"00"
hours = f"00"
elif len(time) <= 5:
seconds = f"0{time[-5]}"
minutes = f"00"
hours = f"00"
elif len(time) == 6:
seconds = f"{time[-6] + time[-5]}"
minutes = f"00"
hours = f"00"
elif len(time) == 8:
seconds = f"{time[-6] + time[-5]}"
minutes = f"0{time[-8]}"
hours = f"00"
elif len(time) == 9:
seconds = f"{time[-6] + time[-5]}"
minutes = f"{time[-9] + time[-8]}"
hours = f"00"
elif len(time) == 11:
seconds = f"{time[-6] + time[-5]}"
minutes = f"{time[-9] + time[-8]}"
hours = f"0{time[-11]}"
elif len(time) == 12:
seconds = f"{time[-6] + time[-5]}"
minutes = f"{time[-9] + time[-8]}"
hours = f"{time[-12] + time[-11]}"
correct_time = f"{hours}:{minutes}:{seconds}"
return correct_time
def reset_time():
pass
def get_frame_count(gif_path):
with Image.open(gif_path) as im:
return im.n_frames
# Create the main window
window = tk.Tk()
window.title("My Kawaii Stopwatch")
# window.geometry("420x400")
window.geometry("600x600")
window.configure(bg="#FFCAC8", padx=50, pady=40)
# bg = ImageTk.PhotoImage(file="hello-kitty-kawaii.gif")
# main_bg_img = tk.Label(window, image=bg)
# main_bg_img.configure(bg="#FFCAC8")
# main_bg_img.place(x=0, y=0)
# main_bg_img.grid(row=1, column=1)
# ------------------------------for making the GIF image show as an animation--(ignore)-------------
bg_gif = "hello-kitty-kawaii.gif"
bg_gif_frames = get_frame_count(gif_path=bg_gif)
im = [tk.PhotoImage(file=bg_gif, format=f"gif -index {i}") for i in range(bg_gif_frames)]
count = 0
anim = None
def animation(count):
global anim
im2 = im[count]
bg_gif_label.configure(image=im2)
count += 1
if count == bg_gif_frames:
count = 0
anim = window.after(100, lambda: animation(count=count))
def stop_animation():
global anim
window.after_cancel(anim)
# ------------------section for making GIF appear as an actual GIF ended-------------------
bg_gif_label = tk.Label(image="")
bg_gif_label.configure(bg="#FFCAC8")
bg_gif_label.grid(row=1, column=1)
pause_button = tk.Button(text="Pause", command=pause_time and stop_animation, bg="#BFECFF")
pause_button.grid(row=2, column=1)
play_button = tk.Button(text="Play", command=play_time and (lambda: animation(count)), bg="#BFECFF")
play_button.grid(row=2, column=2)
reset_button = tk.Button(text="Reset", command=reset_time(), bg="green")
reset_button.grid(row=2, column=0)
time_label = tk.Label(text="00:00:00", font="Consolas")
time_label.grid(row=1, column=1)
window.after(80, func=time_label.config(text=play_time()))
animation(count) # line of code related to GIF animation
window.mainloop()
现在,我定义了一个名为 play_time() 的函数,用于在每次调用时以所需的格式返回正确的时间。我使用 tkinter 的 window.after() 方法在窗口上配置时间标签,并在每 80 毫秒后将其更新为 play_time() 函数返回的字符串。但 window.after() 方法根本没有更新我的时间。这是我的代码: 这是我得到的结果: 这是我得到的结果 请注意,即使我单击播放按钮,结果也是相同的...
如果你想要一个只有 window.after() 错误的最小代码,那么这里是:
import tkinter as tk
from PIL import Image
from stopwatch import Stopwatch
stopwatch_running = False
hours = "0"
minutes = "0"
seconds = "0"
stopwatch = Stopwatch(2)
stopwatch.stop()
def pause_time():
pass
def play_time():
global stopwatch_running, hours, minutes, seconds
if not stopwatch_running:
stopwatch.start()
stopwatch_running = True
time = str(stopwatch)
if len(time) > 5:
seconds = "00"
minutes = f"00"
hours = f"00"
elif len(time) <= 5:
seconds = f"0{time[-5]}"
minutes = f"00"
hours = f"00"
elif len(time) == 6:
seconds = f"{time[-6] + time[-5]}"
minutes = f"00"
hours = f"00"
elif len(time) == 8:
seconds = f"{time[-6] + time[-5]}"
minutes = f"0{time[-8]}"
hours = f"00"
elif len(time) == 9:
seconds = f"{time[-6] + time[-5]}"
minutes = f"{time[-9] + time[-8]}"
hours = f"00"
elif len(time) == 11:
seconds = f"{time[-6] + time[-5]}"
minutes = f"{time[-9] + time[-8]}"
hours = f"0{time[-11]}"
elif len(time) == 12:
seconds = f"{time[-6] + time[-5]}"
minutes = f"{time[-9] + time[-8]}"
hours = f"{time[-12] + time[-11]}"
correct_time = f"{hours}:{minutes}:{seconds}"
return correct_time
def reset_time():
pass
def get_frame_count(gif_path):
with Image.open(gif_path) as im:
return im.n_frames
window = tk.Tk()
window.title("My Kawaii Stopwatch")
window.geometry("600x600")
window.configure(bg="#FFCAC8", padx=50, pady=40)
pause_button = tk.Button(text="Pause", command=pause_time and stop_animation, bg="#BFECFF")
pause_button.grid(row=2, column=1)
play_button = tk.Button(text="Play", command=play_time and (lambda: animation(count)), bg="#BFECFF")
play_button.grid(row=2, column=2)
reset_button = tk.Button(text="Reset", command=reset_time(), bg="green")
reset_button.grid(row=2, column=0)
time_label = tk.Label(text="00:00:00", font="Consolas")
time_label.grid(row=1, column=1)
window.after(80, func=time_label.config(text=play_time()))
animation(count) # line of code related to GIF animation
window.mainloop()
` 伙计们,我已经尝试了所有方法,至少是通过观看 Youtube 视频我能理解的所有方法,但它仍然不起作用。我想我需要一些正弦程序员的专业知识来解决问题,所以请帮助我!
after(delay, func)
计划在指定的 func
(以毫秒为单位)之后调用指定的函数 delay
一次。 因此,如果您想继续定期调用该函数,则需要在函数内部再次调用.after()
。
以下是一个例子:
def update_play_time():
time_label.config(text=play_time()) # update label with play time
window.after(80, update_play_time) # schedule next execution
window.after(80, update_play_time) # start the after loop
您也可以简化
play_time()
如下:
def play_time():
global stopwatch_running, hours, minutes, seconds
if not stopwatch_running:
stopwatch.start()
stopwatch_running = True
seconds = int(stopwatch.duration)
minutes, seconds = divmod(seconds, 60)
hours, minutes = divmod(minutes, 60)
hours = f'{hours:02}'
minutes = f'{minutes:02}'
seconds = f'{seconds:02}'
return f"{hours}:{minutes}:{seconds}"