In this Tkinter tutorial, we will explore how to “hide” your Tkinter Window without closing or destroying it. This allows us to maintain the Window state (the window and its widgets will not have to be re-created), and we can bring this window back (“show” it) whenever we want to.
Benefits of Hiding and Showing Your Tkinter Window
There are several benefits to hiding and showing your Tkinter window rather than closing it.
Firstly, hiding and showing a window allows you to maintain its state. When you hide a window, all of its widgets and their states are preserved. This means that you can bring the window back without having to recreate it from scratch.
Secondly, hiding and showing your window can provide a better user experience. If your application has a long-running process that takes a few seconds to complete, you can hide the window during the process to prevent the user from seeing a frozen window. This can help to make your application appear more responsive and reduce the perceived waiting time.
Thirdly, hiding and showing a window can be useful when you want to display different windows based on user actions. For example, you may want to show a login window when the application starts, and then hide it once the user has logged in. You can then show the main application window instead.
How to Hide Your Tkinter Window
To hide a Tkinter window, you can use the .withdraw()
method. This method removes the window from the screen, but does not destroy it. The window and its widgets remain in memory, so you can bring the window back by calling the .deiconify()
method.
Here is an example that demonstrates how to hide a Tkinter window:
import tkinter as tk
def hide_window():
top_level.withdraw() # hide the top level window
root = tk.Tk()
root.title("Main Window")
# create top level window
top_level = tk.Toplevel(root)
top_level.title("Top Level Window")
# add a button to the main window
button = tk.Button(root, text="Hide Window", command=hide_window)
button.pack(pady=20)
root.mainloop()
In this example, we have created a main window and a top level window. When the “Hide Window” button is clicked, the hide_window()
function is called, which simply hides the top level window using the withdraw()
method.
How to Show Your Tkinter Window
To show a hidden Tkinter window, you can use the .deiconify()
method. This method restores the window to its original state and displays it on the screen.
Here is an example that demonstrates how to show a hidden Tkinter window:
import tkinter as tk
def hide_window():
top_level.withdraw() # hide the top level window
def show_window():
top_level.deiconify() # show the top level window again
root = tk.Tk()
root.title("Main Window")
# create top level window
top_level = tk.Toplevel(root)
top_level.title("Top Level Window")
# add a button to the main window
button = tk.Button(root, text="Hide Window", command=hide_window)
button.pack(pady=20)
# add a button to show the top level window again
show_button = tk.Button(top_level, text="Show Window", command=show_window)
show_button.pack(pady=20)
root.mainloop()
We’ve added a new button show_button
to the top level window which calls the show_window
function when clicked. The show_window
function uses the deiconify
method to show the top level window again.
This marks the end of the “How to Hide / Show your Tkinter Window” Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.
I call a python script file from a menu in diffrent script file. It works fine and exit fine. But when I call it again it does not appera.
Can you help please?