Search code examples
pythonpygtkgtk3

where is `parent` in gtk3?


I'm sorry to place such a simple question, I'm moving from PyGTK to PyGI, and my program has several labels that are made sensitive to mouse click, by adding them in an EventBox; the structure is defined in glade files, and the callbacks are set in the Python code.

In PyGTK, we rely upon being able to find the EventBox by reading the parent property of the Label.

Now I have this simple Python code:

if True:
    import gi
    gi.require_version('Gtk', '3.0')
    from gi.repository import Gtk
else:
    import gtk as Gtk

class MyWindow(Gtk.Window):

    def __init__(self):
        super(MyWindow, self).__init__()

        self.eventbox = Gtk.EventBox()
        self.add(self.eventbox)
        self.label = Gtk.Label("click me")
        self.eventbox.add(self.label)
        print self.label.parent

win = MyWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()

which doesn't do anything, runs for PyGtk, but complains with PyGI, saying that 'Label' object has no attribute 'parent'.

This is not the only place where my code uses the parent field, so I need a generic solution.

A working link to pygi-convert.sh would provide material for study.


in case you wonder, the target software is a botanic database manager.


Solution

  • You're looking for the get_parent() method:

    print(self.label.get_parent())