Search code examples
pythontkinterwidgetparent

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 '_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?

Code:

parent = event.widget.winfo_parent()
parentName = event.widget._nametowidget(parent)

frameParent = parentName.winfo_parent()
frameParentName = frameParent._nametowidget(frameParent)

Solution

  • 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)