pythontkintertoplevel

Why does a Toplevel window get destroyed when the main window is minimized?


from tkinter import *

root = Tk()
root.geometry("500x500")

toplevel = Toplevel()
toplevel.attributes("-toolwindow" , 1)

mainloop()

In this code, when I minimize the main window and open it again, the toplevel window disappears.

Here's an image(GIF) describing my problem:

enter image description here

Is there any way to avoid this?

It would be great if anyone could help me out.

(OS: Windows 10, Python version: 3.9.1, Tkinter version: 8.6)


Solution

  • With the help of acw1668, I found the answer by myself.

    The top-level window does not disappear; instead, it just goes behind all the windows.

    There is a way to bring it back:

    from tkinter import *
    
    root = Tk()
    root.geometry("500x500")
    
    def bring_window_back(e):
        toplevel.attributes('-topmost' , 1)
        toplevel.attributes('-topmost' , 0)
        
    toplevel = Toplevel(root)
    toplevel.attributes("-toolwindow" , 1)
    
    root.bind("<Map>" , bring_window_back)
    
    mainloop()
    

    Note: The <Map> binding may not work properly on linux. If you are searching for a solution for this, please see: Binding callbacks to minimize and maximize events in Toplevel windows

    Hope this helps you all.