How do I get rid of Python Tkinter root window?
Asked Answered
P

8

90

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?

Printing answered 10/9, 2009 at 15:56 Comment(0)
P
130

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()
root.withdraw()

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

root.deiconify()

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

root.destroy()
Phobe answered 10/9, 2009 at 21:2 Comment(5)
So on StackOverflow there are solutions using .destroy(), tkinter.Toplevel(self).destroy(), quit(), tkinter.Toplevel(self).quit(), withdraw(), iconify() to choose from. Nice. Except that not a single one works... I did "top_lev = tkinter.Toplevel(label); label.master.destroy();label.destroy();top_lev.destroy();root = tkinter.Tk();root.withdraw();root.destroy()" which works like a charm (some sarcasm involved).Werewolf
@deusexmachina: they all work if you use them propertly, but they all have different purposes. Though .quit() and .destroy() are effectively identical.Phobe
@BryanOakley I see. Having differently named methods to do the same thing is still quite nasty though IMHO.Werewolf
@deusexmachina: all of these methods do different things. And in the case of destroy() you can use it on any widget. It only behaves like .quit() when you call it on the root window, and even then there are differences. It's just that most apps will never take advantage of the differences.Phobe
Unfortunately, the code here causes a "flash" as the window is created and then hidden. I would like to avoid that.Coniology
K
15

I haven't tested since I don't have any Python/TKinter environment, but try this.

In pure Tk there's a method called "wm" to manage the windows. There you can do something like "wm withdraw .mywindow" where '.mywindow' is a toplevel.

In TkInter you should be able to do something similar to:

root = Tkinter.Tk()
root.withdraw() # won't need this

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

root.deiconify()
Ketchum answered 10/9, 2009 at 20:54 Comment(3)
how do you get the window back on top?Eanore
@IcyFlame: you do root.deiconify() to get the window to reappear.Phobe
thank you sir, I had already asked that question and got an answer: #15968190Eanore
I
11

On OSX, iconify seems to work better:

root = Tkinter.Tk()
root.iconify()
Irritating answered 14/11, 2014 at 14:50 Comment(0)
C
7

If you don't want there to a be "flash" as the window is created, use this slight variation:

import Tkinter as tk
root = tk.Tk()
root.overrideredirect(1)
root.withdraw()
Coniology answered 13/4, 2017 at 1:18 Comment(0)
Y
3

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

Yolondayon answered 5/10, 2020 at 7:13 Comment(0)
A
0

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

Alanis answered 21/4, 2021 at 15:25 Comment(2)
In python 3.x it is called tkinter and not Tkinter.Monocyclic
Um, how is this different from my answer (posted 4 years prior)?Coniology
U
-2

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

import tkinter
tkinter.Tk().withdraw()
Uda answered 4/9, 2019 at 9:0 Comment(1)
Is'nt this the same as was suggested by @BryanOakley ten years ago?Flashy
F
-2
root.deiconify()
root.withdraw()
Fractionize answered 25/1, 2021 at 6:0 Comment(1)
Please tell us why and how it could work.Ousel

© 2022 - 2024 — McMap. All rights reserved.