Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Toplevel in Tkinter: Prevent Two Windows from Opening

Tags:

python

tkinter

Say I have some simple code, like this:

from Tkinter import *
root = Tk()
app = Toplevel(root)
app.mainloop()

This opens two windows: the Toplevel(root) window and the Tk() window.

Is it possible to avoid the Tk() window (root) from opening? If so, how? I only want the toplevel. I want this to happen because I am making a program that will have multiple windows opening, which are all Toplevel's of the root.

Thanks!

like image 924
Rushy Panchal Avatar asked Feb 23 '13 22:02

Rushy Panchal


1 Answers

The withdraw() method removes the window from the screen.
The iconify() method minimizes the window, or turns it into an icon.
The deiconify() method will redraw the window, and/or activate it.

If you choose withdraw(), make sure you've considered a new way to exit the program before testing.
e.g.

from Tkinter import * # tkinter in Python 3

root = Tk()
root.withdraw()

top = Toplevel(root)
top.protocol("WM_DELETE_WINDOW", root.destroy)

but = Button(top, text='deiconify')
but['command'] = root.deiconify
but.pack()

root.mainloop()

The protocol() method can be used to register a function that will be called when the
Toplevel window's close button is pressed. In this case we can use destroy() to exit.

like image 57
Honest Abe Avatar answered Sep 24 '22 23:09

Honest Abe