2016-02-27 73 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