如何在Tkinter中获得主框架的名称[英] How to get the name of the Master Frame in Tkinter

本文是小编为大家收集整理的关于如何在Tkinter中获得主框架的名称的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述


长话短说:Tkinter 中是否有获取小部件主框架名称的功能?

让我再告诉你一点:
有一个Button,名为"BackButton"

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

当我点击这个按钮时,有一个名为"CloseFrame"的函数,它关闭当前的框架(并做一些其他的事情),在本例中是"SCPIFrame".但为此,我需要 Frame 的名称,其中存在 BackButton.有任何想法吗?感谢您的帮助.

推荐答案

从字面上回答你的问题:

<块引用>

是否有获取小部件主框架名称的功能Tkinter?

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

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

本文地址:https://www.itbaoku.cn/post/2091167.html

问题描述


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.

推荐答案

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)