2016-01-12 116 views
-1

我正在製作一個程序,我有一個問題。當使用time.sleep()就像它在代碼中顯示的那樣,我希望它在出現第一個標籤後等待5秒,然後顯示第二個,但是當我按下「開始」按鈕時,它會等待5秒,然後一次顯示兩個標籤。 (我感興趣的代碼只是到了最後,在guess_numberPython Tkinter time.sleep()

下面的代碼:

from tkinter import * 
from tkinter import font 
from datetime import datetime 
import time 

class Window(Frame): 

    def __init__(self, master): 

     Frame.__init__(self, master) 
     self.master = master 
     self.master.resizable(0,0) 
     master.title("Arcade Games") 
     master.geometry("800x600+560+240") 
     now = datetime.now() 
     hour = now.hour 
     minutes = now.minute 
     b = Button(self, text="Guess the number", command=self.new_window,  cursor='hand2', relief='groove') 
     b.pack() 
     self.customFont = font.Font(master, font="Heraldica", size=12) 
     labelTime = Label(self.master, text=str(hour)+" : "+str(minutes), font=self.customFont) 
     labelTime.pack(side='bottom') 


    def new_window(self): 

     id = "Welcome to the 'Guess your number' game!\nAll you need to do is follow the steps\nand I will guess your number!\n\nClick the button to start!!" 
     self.window = Toplevel(self.master) 
     self.window.resizable(0,0) 
     self.label = Label(self.window, text=id, font=self.customFont) 
     self.label.pack(side="top", fill="both", padx=20, pady=20) 
     self.button = Button(self.window, text="Start", relief='groove') 
     self.button.config(width=20, height=2) 
     self.button.bind("<Button-1>", self.guess_number) 
     self.button.pack() 
     self.window.title("Guess the number") 
     self.window.geometry("400x300+710+390") 

    def guess_number(self, event): 

     self.button.destroy() 
     self.label.destroy() 
     labelGuess = Label(self.window, text="Pick any number.\nIt can be 3, 500 or even 1,324,324", font=self.customFont) 
     labelGuess.pack(padx=20, pady=20) 
     time.sleep(5) 
     labelGuess1 = Label(self.window, text="Now double it", font=self.customFont) 
     labelGuess1.pack(padx=20, pady=20) 


if __name__ == "__main__": 
    root = Tk() 
    view = Window(root) 
    view.pack(side="top", fill="both") 
root.mainloop() 

任何幫助表示讚賞!

+0

你有沒有做過任何研究嗎?有關在tkinter中使用'time.sleep'的stackoverflow有很多問題。 –

回答

2

而不使用time.sleep()停止主要任務,儘量安排與Tkinter的的after()事件,像這樣:

def guess_number(self, event): 
    self.button.destroy() 
    self.label.destroy() 
    labelGuess = Label(self.window, text="Pick any number.\nIt can be 3, 500 or even 1,324,324", font=self.customFont) 
    labelGuess.pack(padx=20, pady=20) 
    self.window.after(5000, self.make_guess1_visible) 

def make_guess1_visible(self): 
    labelGuess1 = Label(self.window, text="Now double it", font=self.customFont) 
    labelGuess1.pack(padx=20, pady=20) 
+0

完美的工作,但我如何做到這一點,當我有10個標籤顯示一個又一個? – ToucaN

+0

沒關係!得到它了 :) – ToucaN