如何在Tkinter中获取主框架的名称

问题描述:


简而言之:Tkinter中是否有一个函数可以获取小部件的主框架的名称?


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

让我告诉您更多信息:
有一个名为"BackButton"的按钮

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)

当我单击此按钮时,有一个名为"CloseFrame"的函数,它将关闭当前Frame(并执行其他操作),在本例中为"SCPIFrame".但是为此,我需要其中存在BackButton的Frame的名称. 有任何想法吗?感谢您的帮助.

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.

要从字面上回答您的问题:

To literally answer your question:

是否有一个函数来获取小部件的主框架的名称 Tkinter?

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

winfo_parent正是您需要的.要有用,可以将其与_nametowidget结合使用(因为winfo_parent实际上返回父级的名称).

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)