How do I get rid of Python Tkinter root window?

Viewed 100417

Do you know a smart way to hide or in any other way get rid of the root window that appears, opened by Tk()? I would like just to use a normal dialog.

Should I skip the dialog and put all my components in the root window? Is it possible or desirable? Or is there a smarter solution?

8 Answers

I need to check whether it's withdrawn or not, below is the solution.

import tkinter as tk
root = tk.Tk()
root.withdraw()
print(root.wm_state())
if root.wm_state() == 'withdrawn':  # <----
    root.iconify()
root.mainloop()

withdraw

Removes the window from the screen (without destroying it). To redraw the window, use deiconify. When the window has been withdrawn, the state method returns "withdrawn".

deiconify

redraw the window

iconify

Turns the window into an icon (without destroying it). To redraw the window, use deiconify. Under Windows, the window will show up in the taskbar. When the window has been iconified, the state method returns

state

normal, iconify, withdrawn, icon

This way will work fine:

import Tkinter as tk 
root = tk.Tk() 
root.withdraw()

Or this one:

root = tk.Tk()
root.overrideredirect(1)
root.withdraw()

Two things you must not forget:

  1. Don forget to import the class:

    import tkinter as tk

  2. Place the above commands in the main windows, outside ANY FUNCTION

For Python 3.0 and higher, to hide the window you need to write the following:

import tkinter
tkinter.Tk().withdraw()
Related