[pygtk] Re: Problem with simple custom widget
Jeffrey Barish
jeff_barish at earthlink.net
Mon Feb 18 06:42:07 WST 2008
Joost Behrends wrote:
> I convey this only as a kind of encouragement.
Thanks for sharing this code. It is reassuring to know that someone else
tried and failed to inherit from Entry. And it's clear why you wanted to
inherit from Entry considering all the methods that have to delegate to
methods in Entry.
It seems to me that there are too few examples of inheriting from GTK
classes available, especially considering how many features are not
documented anywhere (e.g., the do_xxx methods). In that spirit, here is my
custom class for attaching a label to an entry. It is very simple and
similar to yours, but perhaps it will be helpful to someone.
By the way, I don't understand why the machinery associated with
__gproperties__ doesn't do more. As far as I can see, using
__gproperties__ does two things for me: I can use the notify signal and I
can use the set_property/get_property methods. I should get an instance
variable and the do_set/do_get methods for free.
"""An entry with a label."""
import gtk
import pygtk
import gobject
HEIGHT = 26
class LabelEntry(gtk.HBox):
__gtype_name__ = 'LabelEntry'
__gproperties__ = {
'tab': (int, "Tab", "Indent of column 1", 0, 100, 60,
gobject.PARAM_READWRITE)
}
def __init__(self, label=None):
gtk.HBox.__init__(self)
gtk.widget_push_composite_child()
self.label = gobject.new(gtk.Label, xalign=0, yalign=0.5)
self.entry = gtk.Entry()
gtk.widget_pop_composite_child()
self.set_property('tab', 60)
if label is not None:
self.label.set_text(label)
self.label.set_size_request(self.tab, -1)
self.entry.set_size_request(-1, HEIGHT)
self.pack_start(self.label, expand=False)
self.pack_start(self.entry)
def set_text(self, value):
self.label.set_text(value)
def do_set_property(self, prop, value):
if prop.name == 'tab':
self.tab = value
self.label.set_size_request(value, -1)
else:
raise AttributeError, 'Unknown property %s' % prop.name
def do_get_property(self, prop):
if prop.name == 'tab':
return self.tab
else:
raise AttributeError, 'Unknown property %s' % prop.name
if __name__ == '__main__':
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.set_title("LabelEntry Test")
window.connect('destroy', gtk.main_quit)
le1 = LabelEntry('First:')
le2 = LabelEntry('Second:')
#le1.props.tab = 80
#le2.set_text('New:')
vbox = gtk.VBox()
vbox.pack_start(le1, expand=False)
vbox.pack_start(le2, expand=False)
window.add(vbox)
window.show_all()
gtk.main()
--
Jeffrey Barish
More information about the pygtk
mailing list