How do I get rid of Python Tkinter root window?
Question:
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?
Answers:
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()
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()
On OSX, iconify seems to work better:
root = Tkinter.Tk()
root.iconify()
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()
For Python 3.0 and higher, to hide the window you need to write the following:
import tkinter
tkinter.Tk().withdraw()
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()
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".
redraw the window
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
normal, iconify, withdrawn, icon
root.deiconify()
root.withdraw()
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:
-
Don forget to import the class:
import tkinter as tk
-
Place the above commands in the main windows, outside ANY FUNCTION
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?
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()
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()
On OSX, iconify seems to work better:
root = Tkinter.Tk()
root.iconify()
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()
For Python 3.0 and higher, to hide the window you need to write the following:
import tkinter
tkinter.Tk().withdraw()
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()
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".
redraw the window
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
normal, iconify, withdrawn, icon
root.deiconify()
root.withdraw()
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:
-
Don forget to import the class:
import tkinter as tk
-
Place the above commands in the main windows, outside ANY FUNCTION