Search code examples
tkinterpython-3.5introspection

How may I find out the Toplevel container of a widget?


I create, as usual, deeply nested frames of widgets which can sometime create other Toplevels. frames.

When I create a new Toplevel frame, I would like to store the originating Toplevel frame by which the new Toplevel has been created but since the button that has created it is deeply nested inside its own frame and has no knowledge of where it is in the hierarchy of its Toplevel, how might it discover its Toplevel container to pass it somewhere else?

Do I have to store it in advance in some sort of a global attribute or is there a way to find it out anyway by using some introspection?

Calling any_widget._root() gives me back the root container (the tkinter.Tk instance)... is there perhaps an amazingly undocumented widget._toplevel method?

EDIT: Deleted (for future reference) the word frame where its use was wrong and confusing.


Solution

  • There is the widget method winfo_toplevel which will return either an instance of Toplevel or Tk.

    top = some_widget.winfo_toplevel()