在Tkinter标签中每隔+1输入一次数字

时间:2016-02-27 09:00:16

标签: python tkinter

我的问题是,每秒循环增加while循环中的数字。我在shell中找到了解决方案,但“time.sleep()”函数对“Tkinter”不起作用。请帮忙!

import time
from tkinter import *

root = Tk()
root.configure(background="grey")
root.geometry("500x500")

#I want to increase money in label every one second  +1  which is displayed,
Money = 100
etiket1 = Label(root,text = str(money)+"$",fg = "Green")
etiket1.pack()

while money < 300:
   money += 1
   time.sleep(1)
   if money == 300:
        break    

#“while”循环不适用于tkinter中的“time.sleep()”

root.mainloop()

2 个答案:

答案 0 :(得分:0)

你通常不想像在GUI程序中那样睡觉,但试试这个:

while money < 300:
    money += 1
    time.sleep(1)
    root.update()

答案 1 :(得分:0)

root.after是time.sleep的tkinter等价物,除了时间是毫秒而不是秒。关于SO的研究有多个例子。

import tkinter as tk
root = tk.Tk()

money = 100
label = tk.Label(root, text = str(money)+"$")
label.grid()

def countup(money):
    money += 1
    label['text'] = str(money)+"$"
    if money < 300:
        root.after(100, countup, money)

root.after(100, countup, money)
root.mainloop()