Python Forum
[Tkinter] Tk window not displaying until loop has completed
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Tk window not displaying until loop has completed
#2
tkinter updates things when your program is idle. Normally this is what you want to happen. If you have something that takes a long time to complete you should break it up into smaller pieces of processing, or run it in a thread or parallel process. This way your GUI is still responsive to user actions.

You will not see anything before mainloop() is called. Here is a much longer delay with a lot less going on.
import tkinter as tk
import time

start = time.time()
root = tk.Tk()
tk.Label(root, text='Hello World!').pack()
print(time.time() - start)
time.sleep(5)
root.mainloop()
If you don't care about having a responsive interface, you can call update() to update your widgets.
import tkinter as tk
import time

start = time.time()
root = tk.Tk()
tk.Label(root, text='Hello World!').pack()
root.update()
print(time.time() - start)
time.sleep(5)
root.mainloop()
This draws the window right away, but the window is unresponsive and you get a busy cursor until the program wakes up from its 5 second nap.
Reply


Messages In This Thread
RE: Tk window not displaying until loop has completed - by deanhystad - Aug-09-2021, 04:30 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Interaction between Matplotlib window, Python prompt and TKinter window NorbertMoussy 3 679 Mar-17-2024, 09:37 AM
Last Post: deanhystad
  Trying to make random image loop in Tk window using python Jediguy18 1 3,259 Dec-30-2020, 04:56 AM
Last Post: deanhystad
  Displaying various layouts in a single window arbiel 6 4,152 Nov-08-2020, 09:21 PM
Last Post: arbiel
  tkinter window and turtle window error 1885 3 6,809 Nov-02-2019, 12:18 PM
Last Post: 1885
  update a variable in parent window after closing its toplevel window gray 5 9,207 Mar-20-2017, 10:35 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020