- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
How do I get rid of the Python Tkinter root window?
Sometimes, while testing a Tkinter application, we may need to hide the Tkinter default window or frame. There are two general methods through which we can either hide our Tkinter window, or destroy it.
The mainloop() keeps running the Tkinter window until it is not closed by external events. In order to destroy the window we can use the destroy() callable method.
However, to hide the Tkinter window, we generally use the “withdraw” method that can be invoked on the root window or the main window.
In this example, we have created a text widget and a button “Quit” that will close the root window immediately. However, we can also use the withdraw method to avoid displaying it on the screen.
#Import the library from tkinter import * #Create an instance of window win= Tk() #Set the geometry of the window win.geometry("700x400") def disable_button(): win.destroy() #Create a Label Label(win,text="Type Something",font=('Helvetica bold', 25), fg="green").pack(pady=20) #Create a Text widget text= Text(win, height= 10,width= 40) text.pack() #Create a Disable Button Button(win, text= "Quit", command= disable_button,fg= "white", bg="black", width= 20).pack(pady=20) #win.withdraw() win.mainloop()
The above python code hides the root window using the withdraw method. However, to destroy the window, we can use the destroy method.
When you click the Quit button, it will hide the root window.
- Related Articles
- How do I get the width and height of a Tkinter window?
- How can I resize the root window in Tkinter?
- How do I close a tkinter window?
- How do I insert a JPEG image into a Python Tkinter window?
- How do I handle the window close event in Tkinter?
- How do I position the buttons on a Tkinter window?
- How do I create a popup window using Tkinter?
- How do I create a popup window in Tkinter?
- How to get rid of widget border in Tkinter?
- How do I create a popup window using Tkinter Program?
- How do I set a minimum window size in Tkinter?
- How do I use Window Manager (wm) attributes in Tkinter?
- How do I open a website in a Tkinter window?
- Python Tkinter – How to position a topLevel() widget relative to the root window?
- How do I use Tkinter in Python to create line-wrapped text that fills the width of the window?