Why does time.sleep pause tkinter window before it opens

后端 未结 1 592
醉话见心
醉话见心 2020-12-04 00:43

I\'m developing a program for a stadium and time.sleep() pauses the program before the window opens instead of when I want it to. What is the explanation for th

相关标签:
1条回答
  • 2020-12-04 01:09

    What time.sleep does is suspend the execution of your program. If you do that 6-16 times, for 1 second each time, before ever calling mainloop(), you're asking it to wait for 6-16 seconds before starting up your GUI.

    You probably don't understand how event loop programming works. Reading through some Tkinter tutorials should get the idea across nicely. If you want a less Tkinter-focused explanation and more information about the details of what's happening and the different ways to get around it, see Why your GUI app freezes.

    At any rate, I think I can guess what you want to do, even though it isn't clear from your question: You want to start the GUI up, and then, every second, replace the Label. To do that, you have to wait while the GUI is running, not before it starts.

    But you can't just call sleep while the GUI is running, either. The GUI can't run while your program is asleep (again, that's what sleep means).

    The easiest way out of this is to turn your loop into a sequence of function calls, each of which schedules the next one to run a second later, using the after method. For example:

    import Tkinter as tk
    import random
    root = tk.Tk()
    
    label = tk.Label(root, text="Navigating To Seat")
    label.pack(pady=10, padx=10)
    
    rand = random.randint(6, 16)
    label2 = None
    
    def add_label():
        global rand
        global label2
        if not rand:
            root.quit()
        if label2:
            label2.pack_forget()
        label2 = tk.Label(root, text="Foward:  " + str(rand) + "m")
        label2.pack()
        rand = rand - 1
        root.after(1000, add_label)
    
    add_label()
    root.mainloop()
    

    When you first call add_label(), it creates the initial label, asks Tkinter to call add_label() again in 1000 milliseconds, and returns. So, a second after you start the loop, it gets called again, which creates the next label and asks Tkinter to call it again a second later. This keeps going until you decrement rand all the way to 0, at which point you call quit instead of after, which ends the main loop, which ends the program.

    There are other things you probably want to fix about this program. For example, instead of destroying and creating a new Widget label each time, you can just change its text—or, maybe even more simply, make rand an IntVar connected to the label, so just updating rand automatically changes the text. Also, for anything less trivial than this program, you'd probably want to replace the global variables with something cleaner—most Tkinter tutorials show you how to use a Frame subclass by about the second or third example, which gives you a convenient place to organize both widgets and member variables like rand.

    0 讨论(0)
提交回复
热议问题