python - python GUI倒数计时器(不要使用类)
问题描述
我的源代码中只有一个问题,lbl_time 不会改变,除此之外一切都运行良好。我只能在我的程序中使用函数,如果有人可以在这个函数程序中帮助我,请这样做。
import tkinter as tk
from datetime import timedelta
import winsound
set time 是一个倒计时功能,我使用 timedelta 来构建时间对象以进行简单操作
def main():
def set_time(hours:int=0, minutes:int=0, seconds:int=0):
end = timedelta(hours=hours, minutes=minutes, seconds=seconds)
one_second = timedelta(seconds=1)
result = end - one_second
new_time = seconds_to_hms(result.seconds)
if result.seconds is 0:
while True:
try:
winsound.PlaySound("Ringtones\\1.cookie clock.wav", winsound.SND_FILENAME)
except KeyboardInterrupt:
break
else:
hours, minutes, seconds = new_time.get('hours'), new_time.get('minutes'), new_time.get('seconds')
time.set(str(hours)+':'+str(minutes)+':'+str(seconds))
root.update()
root.after(1000, lambda : set_time(hours, minutes, seconds))
def seconds_to_hms(seconds:int) -> dict:
hours, minutes, seconds = 0, 0, seconds
if seconds >= 3600:
hours = seconds//3600
seconds = seconds - hours*3600
if seconds >= 60:
minutes = seconds//60
seconds = seconds - minutes*60
result = {'hours': hours, 'minutes': minutes, 'seconds': seconds}
return result
def quit(*args):
root.destroy()
root = tk.Tk()
root.title(string='Timer')
time = tk.StringVar()
root.configure(background='black')
logo = tk.PhotoImage(file='Logo.png')
lbl_logo = tk.Label(root, image=logo, bg='black').pack(side='right')
lbl_timer = tk.Label(root, padx=10, text="Timer", fg='white', bg='black', font='Times 24', anchor='center').pack()
lbl_time = tk.Label(root, text=time, font="Times 38", fg='white', bg='black').pack()
btn_start = tk.Button(root, text='start', bg='gray', fg='black', command=lambda : set_time()).pack()
root.bind('x', quit)
root.after(1000, lambda : set_time(0,0, 3))
root.mainloop()
if __name__ == '__main__':
main()
解决方案
花了一些时间来理解你的问题,但我可能已经解决了你的问题。
从根本上说,您应该在声明中使用
textvariable
而不是text
作为论据lbl_time
。如果你
while loop
被执行,它可能会导致一个无限循环,如果代码在那里分支,你可能希望有一个增量。
请检查以下内容,您可能需要取消注释某些行,希望它确实可以解决您的问题:
import tkinter as tk
from datetime import timedelta
import winsound
def main():
def set_time(hours:int=0, minutes:int=0, seconds:int=0):
end = timedelta(hours=hours, minutes=minutes, seconds=seconds)
one_second = timedelta(seconds=1)
result = end - one_second
new_time = seconds_to_hms(result.seconds)
if result.seconds is 0:
while True:
try:
winsound.PlaySound("Ringtones\\1.cookie clock.wav", winsound.SND_FILENAME)
except KeyboardInterrupt:
break
else:
hours, minutes, seconds = new_time.get('hours'), new_time.get('minutes'), new_time.get('seconds')
time.set(str(hours)+':'+str(minutes)+':'+str(seconds))
root.update()
root.after(1000, lambda : set_time(hours, minutes, seconds))
def seconds_to_hms(seconds:int) -> dict:
hours, minutes, seconds = 0, 0, seconds
if seconds >= 3600:
hours = seconds//3600
seconds = seconds - hours*3600
if seconds >= 60:
minutes = seconds//60
seconds = seconds - minutes*60
result = {'hours': hours, 'minutes': minutes, 'seconds': seconds}
return result
def quit(*args):
root.destroy()
root = tk.Tk()
root.title(string='Timer')
time = tk.StringVar()
root.configure(background='black')
# logo = tk.PhotoImage(file='Logo.png')
# lbl_logo = tk.Label(root, image=logo, bg='black').pack(side='right')
lbl_timer = tk.Label(root, padx=10, text="Timer", fg='white', bg='black', font='Times 24', anchor='center').pack()
lbl_time = tk.Label(root, textvariable=time, font="Times 38", fg='white', bg='black').pack() #changed text to textvariable
btn_start = tk.Button(root, text='start', bg='gray', fg='black', command=lambda :set_time(0,0,3700)).pack()
root.bind('x', quit)
#root.after(1000, lambda : set_time(0,0, 3))
root.mainloop()
if __name__ == '__main__':
main()
推荐阅读
- html - 如何摆脱具有必需属性的 HTML 表单
- javascript - 使用 React hooks (useRef) 将类组件转换为功能组件——如何避免“未定义”错误
- python - 重命名文件以包含基于前一个工作日的后缀
- database - 池中的理想连接数是多少?
- c# - 我可以拥有一个具有多个阅读器的 C# System.IO.Pipelines.Pipe 吗?
- c - pthread_attr_setschedparam() 返回非零但 errno = 成功 ...(Linux、C、Ubuntu 18.04.4)
- vba - 当 DocVariable 字段包含在 TextBox 中时,如何使用 VBA 用字符串值填充 DocVariable
- postgresql - 将包含列表的记录值与 Postgres 中的列值进行比较
- java - Java、Apache POI、XSSFWorkbook 极慢
- javascript - ReactJs 函数式组件将函数添加到 eventListener 然后调用它