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

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


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

top = some_widget.winfo_toplevel()


I am not sure, but I think that by toplevel you meant the parent of the widget: if you created nested widgets like

root = tk.Tk()frame = tk.Frame(root)label = tk.Label(frame, text="text")

then frame is the parent of label and root is the parent of frame.

To access the parent of a widget, you can use the winfo_parent method to get the parent name, and then, the nametowidget method to get the actual widget corresponding to the name:

any_widget.nametowidget(any_widget.winfo_parent())