views:

139

answers:

1

Consider this Python program which uses PyGtk and Hippo Canvas to display a clickable text label. Clicking the text label replaces it with a Hippo CanvasEntry widget which contains the text of the label.

import pygtk
pygtk.require('2.0')
import gtk, hippo

def textClicked(text, event, row):
    input = hippo.CanvasEntry()
    input.set_property('text', text.get_property('text'))
    parent = text.get_parent()
    parent.insert_after(input, text)
    parent.remove(text)

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

    text = hippo.CanvasText(text=u'Some text')
    text.connect('button-press-event', textClicked, text)
    root.append(text)

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

    canvas.show()
    window.show()

    gtk.main()

if __name__ == '__main__':
    main()

How can the CanvasEntry created when the text label is clicked be automatically focused at creation time?

+2  A: 

Underneath the CanvasEntry, there's a regular old gtk.Entry which you need to request the focus as soon as it's made visible. Here's a modified version of your textClicked function which does just that:

def textClicked(text, event, row):
    input = hippo.CanvasEntry()
    input.set_property('text', text.get_property('text'))
    entry = input.get_property("widget")
    def grabit(widget):
        entry.grab_focus()
    entry.connect("realize", grabit)
    parent = text.get_parent()
    parent.insert_after(input, text)
    parent.remove(text)
Glyph