简体   繁体   English

Python Tkinter time.sleep()

[英]Python Tkinter time.sleep()

I am making a program and i have a problem. 我正在编写程序,但是有问题。 When using time.sleep() like it shows in the code, i would like it to wait 5 secs after the first label appears and then show the second one, but instead when i press the 'start' button it waits 5 secs and then displays both labels at once. 使用代码中显示的time.sleep()时,我希望它在第一个标签出现后等待5秒钟,然后显示第二个标签,但是当我按“开始”按钮时,它等待5秒钟,然后同时显示两个标签。 (The code i am interested in is just at the end, in guess_number ) (我感兴趣的代码只是在最后, guess_number

Here's the code: 这是代码:

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()

Any help is appreciated! 任何帮助表示赞赏!

Rather than use time.sleep() to stop the main task, try scheduling an event with tkinter's after() , like so: 与其使用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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM