stop tkinter window from freezing while program is sleeping

Calling time.sleep() will stop your program doing anything until it finishes sleeping. You need Tkinter to keep processing events until it should run the next part of your code.

To do that, put the next part of your code in a separate function, and get Tkinter to call it when it’s ready. Typically, you want this to happen when the user triggers it (e.g. by clicking a button), so you need to bind it to an event (docs). If you actually want it to happen after a fixed time, you can use the .after() method on any tkinter widget (docs).

GUI programming takes a bit of getting used to. You don’t code as a series of things happening one after the other, you write separate bits of code which are triggered by what the user does.

Terminology note: if your Python files import each other, you have three modules, but it’s still all one program. Talking about the “first program” will confuse people.

Leave a Comment