Question

Considérez ce programme Python qui utilise PyGtk et Hippo Canvas pour afficher une étiquette de texte cliquable. En cliquant sur l'étiquette de texte, vous la remplacez par un widget Hippo CanvasEntry qui contient le texte de l'étiquette.

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()

Comment CanvasEntry créé lors du clic sur l'étiquette de texte peut-il être automatiquement concentré au moment de la création?

Était-ce utile?

La solution

Sous CanvasEntry , vous trouverez un ancien gtk.Entry classique dont vous avez besoin pour demander le focus dès qu'il est visible. Voici une version modifiée de votre fonction textClicked qui ne fait que cela:

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)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top