嘿大家,我在 tkinter 中制作计时器时遇到问题,但我无法使用 time.sleep()
所以我使用 .after()
并且我遇到了新问题,我输入了一个条目,我希望条目编号为 * 60,并且在设定的时间之后,将写入一条文本,显示 >> 时间已结束!
,但是,60 应该如何表示换算成秒?我的代码:
from tkinter import *
from playsound import playsound
from time import sleep
import time
def jik():
a = int(text.get())
app.after(a * 600)
Label(app,text="time is over").pack()
app = Tk()
app.minsize(300,300)
app.maxsize(300,300)
text = Entry(app,font=20)
text.pack()
Button(app,text="start",command=jik).pack()
app.mainloop()
例如,如果我按数字 1,不到一秒就>>时间结束
最佳答案
after
命令以毫秒为单位接收输入,因此将其乘以 1000 将其转换为秒。
此外,我刚刚做了一个小例子,当时钟滴答作响时为您显示倒计时:
# Usually it is a good idea to refrain from importing everything from the tkinter
# package, as to not pollute your namespace
import tkinter as tk
root = tk.Tk() # Customary to call your Tk object root
entry = tk.Entry(root)
entry.pack()
curtime = tk.Label(root)
curtime.pack()
is_running = False # Global variable to ensure we can't run two timers at once
def countdown(count):
global is_running
if count > 0:
curtime['text'] = f'{count:.2f}' # Update label
# Call the countdown function recursively until timer runs out
root.after(50, countdown, count-0.05)
else:
curtime['text'] = 'time is over'
is_running = False
def btn_press():
global is_running
if is_running:
return
cnt = int(entry.get())
is_running = True
countdown(cnt)
tk.Button(root, text='start', command=btn_press).pack()
root.minsize(300, 300)
root.maxsize(300, 300)
root.mainloop()
关于python - tkinter .after() 秒和分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74361523/