Skip to content Skip to sidebar Skip to footer

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. Error: AttributeError: 'str' object has no attribute '_nametowid

Solution 1:

grandparent = event.widget.master.master

See the answer here.

Solution 2:

http://effbot.org/tkinterbook/widget.htm

Mentions as below, winfo_parent() is a method to get parent's name.

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)

Solution 3:

Your second call to _nametowidget() is different than your first. Your fourth line is bad, since you're basing the call to _nametowidget() on a string (frameParent) instead of on a widget (parentName). This is partly because your naming convention of "widget" and "widgetName" is reversed...

frameParentName = frameParent._nametowidget(frameParent)

                   ^^^^^^^^^^^ -- thisis the name string. you need a widget

I would slightly rewrite your code as follows:

parentName = event.widget.winfo_parent()
parent     = event.widget._nametowidget(parentName) #event.widget is your widgetframeParentName = parent.winfo_parent()
frameParent     = parent._nametowidget(frameParentName) #parent is your widget

Post a Comment for "Getting The Parent Of A Parent Widget In Python Tkinter"