在 GTK 中转发按键
我正在为 Gedit 插件编写一些代码。我使用的是 Python,接口(显然)是 GTK。
所以,我遇到的问题非常简单:我有一个搜索框(gtk.Entry),在下面我有一个结果框(gtk.TreeView)。在搜索框中输入内容后,您会立即看到一堆结果,我希望用户能够按向上/向下键选择一个,然后按 Enter 键选择它,然后完成。问题是,我似乎找不到将向上/向下按键转发到 TreeView 的方法。目前我有这段代码:
def __onSearchKeyPress(self, widget, event):
"""
Forward up and down keys to the tree.
"""
if event.keyval in [gtk.keysyms.Up, gtk.keysyms.Down]:
print "pressed up or down"
e = gtk.gdk.Event(gtk.gdk.KEY_PRESS)
e.keyval = event.keyval
e.window = self.browser.window
e.send_event = True
self.browser.emit("key-press-event", e)
return True
我可以清楚地看到我正在接收正确类型的事件,但我发送的事件被 TreeView 忽略。有什么想法吗?
预先感谢人们。
I'm writing a bit of code for a Gedit plugin. I'm using Python and the interface (obviously) is GTK.
So, the issue I'm having is quite simple: I have a search box (a gtk.Entry) and right below I have a results box (a gtk.TreeView). Right after you type something in the search box you are presented a bunch of results, and I would like the user to be able to press the Up/Down keys to select one, Enter to choose it, and be done. Thing is, I can't seem to find a way to forward the Up/Down keypress to the TreeView. Currently I have this piece of code:
def __onSearchKeyPress(self, widget, event):
"""
Forward up and down keys to the tree.
"""
if event.keyval in [gtk.keysyms.Up, gtk.keysyms.Down]:
print "pressed up or down"
e = gtk.gdk.Event(gtk.gdk.KEY_PRESS)
e.keyval = event.keyval
e.window = self.browser.window
e.send_event = True
self.browser.emit("key-press-event", e)
return True
I can clearly see I'm receiving the right kind of event, but the event I'm sending gets ignored by the TreeView. Any ideas?
Thanks in advance people.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这不是问题的正确答案(我不知道如何转发按键),但有另一种解决方案可以解决您的问题。
直接操作 TreeView 光标/选择,例如:
Not a proper answer to the question (I don't know how to forward key presses), but there's an alternative solution to your problem.
Manipulate the TreeView cursor/selection directly, for example:
您是否将
key-press-event
包含在小部件允许接收的事件列表中?您可以通过致电来做到这一点Did you include the
key-press-event
in the list of events the widget is allowed to receive? You can do that by calling