考虑这个使用PyGtk和Hippo Canvas显示可点击文本标签的Python程序。单击文本标签会将其替换为包含标签文本的Hippo CanvasEntry小部件。
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()
单击文本标签时创建的CanvasEntry如何在创建时自动聚焦?
答案 0 :(得分:2)
在CanvasEntry
下方,有一个常规的旧gtk.Entry
,您需要在看到它后立即请求焦点。以下是textClicked
函数的修改版本,它只执行以下操作:
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)