How to Get Rid of Python Tkinter Root Window

How do I get rid of Python Tkinter root window?

Probably the vast majority of of tk-based applications place all the components in the default root window. This is the most convenient way to do it since it already exists. Choosing to hide the default window and create your own is a perfectly fine thing to do, though it requires just a tiny bit of extra work.

To answer your specific question about how to hide it, use the withdraw method of the root window:

import Tkinter as tk
root = tk.Tk()

If you want to make the window visible again, call the deiconify (or wm_deiconify) method.


Once you are done with the dialog, you can destroy the root window along with all other tkinter widgets with the destroy method:


How To Hide Tkinter python Gui

To hide the default root window you can use


and to make it visible again you can use


how to remove an extra window in python Tkinter?

This causes a window to be created:

class Toplevel1(tk.Tk):
def __init__(self, top=None, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
app = Toplevel1(root)

And this causes a window to be created:

root = tk.Tk()

If you don't want Toplevel1 to be a separate window, don't inherit from tk.Tk. Instead, you can inherit from tk.Frame, and then you can call pack, place, or grid to add this to the root window.

However, it looks like you're intending for your Toplevel1 to be the true root window, so you can remove root = tk.Tk(). You can then do app.mainloop() rather than root.mainloop() You'll also have to make a few other adjustments, such as using self rather than top inside Toplevel1.__init__.

Put another way, if you want only one window then either inherit from tk.Tk or create an instance of tk.Tk, but don't do both.

How do I close a tkinter window?

You should use destroy() to close a Tkinter window.

from Tkinter import * 
#use tkinter instead of Tkinter (small, not capital T) if it doesn't work
#as it was changed to tkinter in newer Python versions

root = Tk()
Button(root, text="Quit", command=root.destroy).pack() #button to close the window



The above line just bypasses the root.mainloop(), i.e., root.mainloop() will still be running in the background if quit() command is executed.


While destroy() command vanishes out root.mainloop(), i.e., root.mainloop() stops. <window>.destroy() completely destroys and closes the window.

So, if you want to exit and close the program completely, you should use root.destroy(), as it stops the mainloop() and destroys the window and all its widgets.

But if you want to run some infinite loop and don't want to destroy your Tkinter window and want to execute some code after the root.mainloop() line, you should use root.quit(). Example:

from Tkinter import *
def quit():
global root

root = Tk()
while True:
Button(root, text="Quit", command=quit).pack()
#do something

See What is the difference between root.destroy() and root.quit()?.

tkinter: how to not display the empty root window when creating it

Use the following two methods to hide or show the root window.

def hide(root):

def show(root):

When you center the root window its size is (1, 1), you should give window size to center method.

lambda is not needed here, use command=root.destroy.

import Tkinter as tk

def center(win, width, height):
frm_width = win.winfo_rootx() - win.winfo_x()
win_width = width + 2 * frm_width
titlebar_height = win.winfo_rooty() - win.winfo_y()
win_height = height + titlebar_height + frm_width
x = win.winfo_screenwidth() // 2 - win_width // 2
y = win.winfo_screenheight() // 2 - win_height // 2
win.geometry('{}x{}+{}+{}'.format(width, height, x, y))

def show(root):

def hide(root):

def showDialog():
print "tkinter"
root = tk.Tk()
root.title("Say Hello")
label = tk.Label(root, text="Hello World")
label.pack(side="top", fill="both", expand=True, padx=20, pady=20)
button = tk.Button(root, text="OK", command=root.destroy)
button.pack(side="bottom", fill="none", expand=True, padx=10, pady=10)
center(root, width=200, height=200)


Tkinter: Proper way to withdraw Tkinter root window with multiple instances

I found the solution;


Putting the root and withdraw before the filedialog and then putting the above code before the listbox is generated, fixes the issue.

How do I remove all elements in a tkinter GUI?

You can either remove or destroy all children of the root iteratively (eg: for child in root.winfo_children(): child.destroy()), or you can create a single frame directly in the root and make all other widgets children of that frame, and then just remove or delete the frame.

Remove minimizd button in tkinter root window

This is code:

import tkinter as tk

root = tk.Tk()

def create_new_window():
tk.Label(win,text=' Hello World ').pack()

label1 = tk.Label(root, text='Click the button')

b1=tk.Button(root,text='New Window',command=create_new_window)

Related Topics

Leave a reply