gpt4 book ai didi

python 脚本在 x 秒后运行,但是当插入 tkinter 代码时,它只运行一次

转载 作者:行者123 更新时间:2023-12-01 08:10:39 26 4
gpt4 key购买 nike

What is the best way to repeatedly execute a function every x seconds in Python?

我已经尝试了上面链接中发布的解决方案,但没有一个可以帮助我达到预期的结果。

上面的代码在控制台上按提到的秒数多次打印“Doing stuff...”,即 5,但是当我添加 window() 行(用于显示消息的 tkinter 代码)时,代码仅运行一次而不是运行随时再来。

请帮忙。我想根据系统时钟在特定时间一次又一次运行 tkinter 代码,但现在我只是尝试在 x 秒后执行它。

任何帮助对我来说都意义重大。谢谢

最佳答案

搜索 tkinter .after 方法。

这将允许您每 x 秒运行一次命令。

您的 tkinter 代码仅运行一次的问题是,因为它首先设置然后进入循环 (root.mainloop()) ,因此永远不会返回到您的代码以再次显示任何内容。

示例:tkinter: how to use after method

关于python 脚本在 x 秒后运行,但是当插入 tkinter 代码时,它只运行一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55274784/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com