How to show a window that was hidden using the "withdraw" method in Tkinter?



Tkinter withdraw method hides the window without destroying it internally. It is similar to the iconify method that turns a window into a small icon. Let us suppose we want to reveal the hidden window during the execution of an application then we can use deiconify() method. It can be invoked with the window or frame of a widget in the application.

Example

In this example, we will define a button in a Toplevel window (Popup Window) which can be the trigger to reveal the main window.

#Import the library
from tkinter import *
from tkinter import ttk

#Create an instance of Tkinter frame
win= Tk()

#Set the window geometry
win.geometry("750x200")

#Create a Label
Label(win, text= "Tkinter is a GUI Library in Python", font=('Helvetica 15 bold')).pack(pady=20)

#Define a function to show the Main window
def show_win():
   win.deiconify()

#Create another Toplevel Window
new_win= Toplevel(win)
new_win.geometry("700x250")
new_win.title("NEW WINDOW")

#Hide the Main Window
win.withdraw()

#Create a Button to Hide/ Reveal the Main Window
button= ttk.Button(new_win, text="Show" ,command= show_win)
button.pack(pady=50)

win.mainloop()

Output

Running the above code will show the output as,

When we click the button "Show", it will display the Main window.

raja
Published on 03-May-2021 11:25:28
Advertisements