How do I get rid of the Python Tkinter root window?

PythonTkinterGUI-Programming

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.

Example

#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.

Output

When you click the Quit button, it will hide the root window.

raja
Published on 04-Mar-2021 18:19:29
Advertisements