Search code examples
tkinterwidget

How to get the name of the Master Frame in Tkinter



to cut a long story short: Is there a function to get the name of the Master Frame of a widget in Tkinter?

Let me tell you a little bit more:
There is a Button, named "BackButton"

self.BackButton = Button(self.SCPIFrame, text = "Back", command = self.CloseFrame)
self.BackButton.place(x = 320, y = 320, anchor = CENTER)

When I click on this Button, there is a function named "CloseFrame", which closes the current Frame (and doing some other stuff), in this case "SCPIFrame". But for this, I need the name of the Frame, in which the BackButton is present. Any ideas? Thanks for helping.


Solution

  • To literally answer your question:

    Is there a function to get the name of the Master Frame of a widget in Tkinter?

    winfo_parent is exactly what you need. To be useful, you can use it combined with _nametowidget (since winfo_parent actually returns the name of the parent).

    parent_name = widget.winfo_parent()
    parent = widget._nametowidget(parent_name)