Reputation: 9738
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.
Upvotes: 1
Views: 1021
Reputation: 4114
You must use the getter method from Gtk.Widget get_parent().
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.get_parent()
win = MyWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
After execution:
<Gtk.EventBox object at 0x7f802a597910 (GtkEventBox at 0x55a88239a130)>
Upvotes: 1
Reputation: 43226
You're looking for the get_parent()
method:
print(self.label.get_parent())
Upvotes: 1