我在tkinter窗口中创建了令牌,该令牌每10秒更改一次值。我想改善它并添加进度条。我了解现成的库,但是我想使用我的个人代码。我的问题是如何减慢循环速度,以便根据循环进度等将当前值传送到字符串变量,并在一秒钟后将另一个值传送给我。我的代码仅需要此“停止器”。我可以使用10个不同的函数,而只能触发after(),但它看起来并不好,而且一段时间后我的程序也会崩溃。
"qualified name is not allowed".
答案 0 :(得分:0)
编辑:这是一种通用方法,应给出如何解决此问题的想法。它包含所有必要的部分,但是必须在代码的正确位置实现。
要确保tkinter窗口在更新阶段不会冻结,您必须在另一个线程中进行进度条更新。
要在一定时间后重复调用该方法,可以使用time.sleep()
方法和递归方法一次又一次地调用自身。
import threading
import time
def change_progressbar(bar):
# If there is more progress bar points to display, then do it
if bar > 0:
print(bar * ' l') # Change your var here instead of printing
time.sleep(10) # Wait 10 seconds
change_progressbar(bar - 1) # Call itself again with one less point in a progress bar
# Let's call a method in new thread, with argument bar=10, which
# defines how many progress bar points there will be at start.
progressbar_thread = threading.Thread(target=change_progressbar, args=[10])
progressbar_thread.start()
# Output:
# l l l l l l l l l l
# l l l l l l l l l
# l l l l l l l l
# l l l l l l l
# l l l l l l
# l l l l l
# l l l l
# l l l
# l l
# l
答案 1 :(得分:-1)
您正在寻找的“停止器”是time.sleep()
sleep函数需要一个参数,即等待的秒数。 也可以是0.5之类的浮点数
您可以这样使用它:
import time
while True:
time.sleep(1)
print("One second has passed")
此代码将每秒打印一些内容。