在 Python Tkinter 中获取父小部件的父级 [英] Getting the parent of a parent widget in Python Tkinter
问题描述
我正在尝试获取小部件的父级,然后获取该小部件的父级.但是每次我尝试时都会出错.
I am trying to get the parent of a widget then get the parent of that widget. But Everytime I try to I get a error.
错误:
AttributeError: 'str' object has no attribute '_nametowidget'
为什么给我这个错误.有人可以向我解释为什么我会收到此错误并帮助我修复它吗?
Why is it giving me that error. Can someone explain to me why I am getting this error and help me to fix it?
代码:
parent = event.widget.winfo_parent()
parentName = event.widget._nametowidget(parent)
frameParent = parentName.winfo_parent()
frameParentName = frameParent._nametowidget(frameParent)
推荐答案
http://effbot.org/tkinterbook/widget.htm
如下所述,winfo_parent() 是获取父母姓名的方法.
Mentions as below, winfo_parent() is a method to get parent's name.
您得到的错误意味着 event.widget 没有名为 _nametowidget 的方法.所以你不能把它作为一个函数来调用.
The error you get means that event.widget doesn't has the method named _nametowidget. So you could not call that as a function.
您可以尝试以下代码来获取父级.
You may try codes below to get the parent.
parent = event.widget.winfo_parent()
from Tkinter import Widget
Widget._nametowidget(parent)
这篇关于在 Python Tkinter 中获取父小部件的父级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!