Is there any way to assign `tkinter.Toplevel` to a variable without packing it?

Question:

When you call tkinter.Toplevel or assign it to a variable, it immediately packs (shows up in the screen). The idea behind this concept is, there will be multiple widgets placed on the top level and those widgets will take some time to be packed (to get ready). So, I have to declare a master window for those widgets which is the tkinter.Toplevel but if firstly I call it or assign it to a variable it automatically shows up in the screen as blank. Can I make the top level window packed together with the widgets within when they are all ready?

from tkinter import *
import time
import threading

def func():
    top_level = Toplevel(root)
    label_text = 0
    button_text = 0
    for i in range(10):
        time.sleep(1)
        label_text += 1
        button_text += 2

    label = Label(top_level, text=label_text)
    button = Button(top_level, text=button_text)

    label.pack(pady=20, padx=20)
    button.pack(pady=20, padx=20)

root = Tk()

threading.Thread(target=func).start()

root.mainloop()
Asked By: duruburak

||

Answers:

packed isn’t the right terminology (the correct terminology is "mapped"), but if you don’t want it to appear you can call withdraw/wm_withdraw to prevent it from appearing on the screen.

top_level = Toplevel(root)
top_level.withdraw()

When you want it to appear, call deiconify/wm_deiconify:

top_level.deiconify()
Answered By: Bryan Oakley