Focusing Hippo CanvasEntry



I've just started to play around with Hippo Canvas tonight.  It's been
quite easy so far, but one thing that's got me stumped is how to deal
with focus for canvas items.  Specifically, if I add a new CanvasEntry
to a canvas somewhere, can I make focus go to that item?

Attached is the little program I've been playing around with so far.  If
you run it and click on any text label, it'll be replaced with an entry
item - these newly created entries are the ones I'd like to gain focus as
soon as they're added.

I didn't see any other posts about Hippo Canvas in the archives, but I
also didn't see another list about the project.  If there's a more
appropriate forum for questions about it, please let me know.

Jean-Paul
import pygtk
pygtk.require('2.0')
import gtk, hippo

EVEN = 0xffffffffL
ODD = 0xddddddffL

def makeText(s, row):
    t = hippo.CanvasText(
        text=s, background_color=[EVEN, ODD][row % 2],
        size_mode=hippo.CANVAS_SIZE_FULL_WIDTH, border=3)
    t.connect('button-press-event', textClicked, row)
    return t



def inputSubmitted(input, event, row):
    if event.key == hippo.KEY_RETURN:
        # Convert it back to a label
        text = makeText(input.get_property('text'), row)
        parent = input.get_parent()
        parent.insert_after(text, input, hippo.PACK_EXPAND)
        parent.remove(input)



def textClicked(text, event, row):
    # Convert it to an input
    input = hippo.CanvasEntry()
    input.set_property('text', text.get_property('text'))
    input.connect('key-press-event', inputSubmitted, row)
    parent = text.get_parent()
    parent.insert_after(input, text, hippo.PACK_EXPAND)
    parent.remove(text)



def main():
    canvas = hippo.Canvas()
    root = hippo.CanvasBox(orientation=hippo.ORIENTATION_HORIZONTAL)
    canvas.set_root(root)

    # Add some rows and columns
    for c in xrange(2):
        row = hippo.CanvasBox(orientation=hippo.ORIENTATION_VERTICAL)
        for r in xrange(5):
            text = makeText(u'%dx%d' % (c, r), r)
            row.append(text, hippo.PACK_EXPAND)
        root.append(row, hippo.PACK_EXPAND)

    window = gtk.Window()
    window.connect('destroy', lambda ignored: gtk.main_quit())
    window.add(canvas)

    canvas.show()
    window.show()

    gtk.main()

main()


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]