Tk's default Toplevel - counterintuitive

Chad Netzer chad at vision.arc.nasa.gov
Fri Aug 20 01:47:09 CEST 1999


Stuart Reynolds wrote:

> Does anyone know how to tell if Tk has already instaniated a Toplevel
> window? I want to be able to open several Toplevel windows in any order.

Try this:

from Tkinter import *

def makeNewWindow():
    from Tkinter import _default_root
    if _default_root is not None:
        master=Toplevel()
    else:
        master = None
    f = Frame(master=master)
    b = Button(master=f, text="Hello")
    b.pack(side=LEFT, fill=BOTH)
    Pack.config(f)

makeNewWindow()
makeNewWindow()
makeNewWindow()

NOTE - The "from Tkinter import _default_root" can be avoided by importing
Tkinter directly at the beginning (ie. "import Tkinter"), and accessing it
through
the Tkinter module.  Otherwise, the _default_root reference you have won't get

updated when Tkinter changes it.






More information about the Python-list mailing list