2016-02-27 42 views
0

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

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  

#“而”循环不工作“time.sleep()”中的Tkinter

root.mainloop() 
+0

[如何使用Tkinter的创建一个定时器?](http://stackoverflow.com/questions/2400262/how-to-create-a-timer-using-tkinter) –

回答

0

你通常不会想要做一个这样的睡在一个GUI程序,但试试这个:

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

root.after是Tkinter的等效time.sleep的,但时间是毫秒,而不是秒。 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() 
+0

由于可能的重复,我朋友,我一直在寻找一个很好的解释,但至少我找到了一些东西,这对我很有用,谢谢 – KAMATLI