[pygtk] Problem with simple custom widget

Joost Behrends webmaster at h-labahn.de
Sun Feb 17 19:17:55 WST 2008


Hello Jeffrey,

a remark slightly out of topic: what you tried will probably have been done by
many others too. I use a set of classes and functions like 'makeButton',
'frameWidget', 'scalingPixpuf' (which remembers the pixbuf primarily created
as kind of 'source' for scaling) and so on in a midsize project. 

That module containes classes 'TitledEntry' and 'UnderTitledEntry', the second
inheriting from the first, because it appeared useful for me to separate the
convenience function, which wraps the creation of the label, from the function
wrapping the layout.

They belong to the first of what i did for that project, and worked well (they exist for convenience only - thus some of their attributes stay special to my project). And i remember, that i ran into difficulties trying to let inherit TitledEntry from gtk.Entry.
Here they are:

class TitledEntry():
    """
    Composed of 3 widgets: An entry, it's title label,
    and an optional DefaultButton (makes the current text to a default)
    These widgets are visually free, there is no geometry in this class.
    """
    labelFt = panfont("Verdana bold 10")
    entryFt = panfont("Verdana bold 10")

    defaultStr = ""
    def storeContent(q, widget=None): q.defaultStr = q.get_text()
    def remember(q): q.set_text(q.defaultStr)

    def __init__(q, title, cWidth = -1, default = False, lbColor = white):
        q.entry = gtk.Entry()
        q.entry.set_width_chars(cWidth)
        q.entry.modify_font(q.entryFt)
        q.entry.modify_base(0, colop('#fffaf8'))
        q.entry.set_has_frame(True)
        q.label = gtk.Label(title)
        q.label.set_single_line_mode(True)
        q.label.modify_font(q.labelFt)
        if type(lbColor) in (str, unicode): lbColor = colop(lbColor)
        q.label.modify_fg(0, lbColor)

        q.default = default
        if default:
            q.button = gtk.Button()
            q.button.set_property('can-focus', False)
            q.button.set_size_request(31,31)
            imo = gtk.Image(); imo.set_from_pixbuf(exclIm)
            q.button.set_image(imo)
            q.button.set_alignment(0.5,0.5)   # gtk.Image is alignable     
            q.button.connect("clicked", q.storeContent)
    def show(q):
        q.entry.show()
        q.label.show()
        if q.default: q.button.show()
    def set_text(q, sa): q.entry.set_text(sa)
    def get_text(q): return q.entry.get_text()
    def select_region(q, i, j): q.entry.select_region(i,j)
    def modify_font(q, s): q.entry.modify_font(s)
    def set_size_request(q, x, y): q.entry.set_size_request(x, y)
    def grab_focus(q): q.entry.grab_focus()

class UnderTitledEntry(TitledEntry, gtk.Table):
    """  A certain layout for a Titled Entry """
    def __init__(q, title, cWidth = -1, default = False, lbColor = white):
        TitledEntry.__init__(q, title, cWidth, default, lbColor)
        gtk.Table.__init__(q)
        q.labelAn = (0.0, 0.0, 0.001, 0.8)
        al = gtk.Alignment(*q.labelAn)        
        al.add(q.label); al.show()
        q.attach(al, 0, 2, 1, 2, yoptions = 0)
        q.attach(q.entry, 0, 1, 0, 1)
        if default: q.attach(q.button, 1, 2, 0, 1, xoptions = 0, yoptions = 0)         
        q.set_focus_chain((q.entry,))
    def show(q):
        super(UnderTitledEntry, q).show()
        super(gtk.Table, q).show()    

(panfont is pango.FontDescription, colop gtk.gdk.color_parse of course). I convey this only
as a kind of encouragement.

Good luck, Joost


More information about the pygtk mailing list