【问题标题】:Trying to code a 5 second countdown timer in tkinter尝试在 tkinter 中编写一个 5 秒倒计时计时器
【发布时间】:2023-04-02 09:45:01
【问题描述】:

这是我写的代码,我不明白为什么,但我的画布上什么也没有。

def countdown():
    for n in range(5, 0, -1):
        nr = Label(root, text=n, font="helvetica 25 bold", bg="white")
        nr.place(x=590, y=420)
        nr.destroy()

【问题讨论】:

  • 可能是因为你立即销毁它?
  • 如果这是你写的所有代码,你永远不会导入任何东西,永远不会创建根窗口,永远不会调用mainloop,也永远不会调用你的函数。
  • 要添加到以前的答案,因为你有这个循环,GUI 没有机会用新标签重绘。每个数字之间也没有时间延迟,因此从人类的角度来看,这些数字是不可见的。
  • 这能回答你的问题吗? Making a countdown timer with Python and Tkinter?

标签: python tkinter countdown


【解决方案1】:

使用 root.after(timer, function)。最好的方法是调用root.after(1000, your_func, count-1) your_func 应该将标签的文本配置为“count”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-04
    • 1970-01-01
    相关资源
    最近更新 更多