在 GTK 中转发按键

发布于 2024-08-26 08:26:10 字数 733 浏览 11 评论 0原文

我正在为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奢欲 2024-09-02 08:26:10

这不是问题的正确答案(我不知道如何转发按键),但有另一种解决方案可以解决您的问题。

直接操作 TreeView 光标/选择,例如:

path, column = browser.get_cursor()
browser.set_cursor((path[0] + 1,)) # Down

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:

path, column = browser.get_cursor()
browser.set_cursor((path[0] + 1,)) # Down
三月梨花 2024-09-02 08:26:10

您是否将 key-press-event 包含在小部件允许接收的事件列表中?您可以通过致电来做到这一点

browser.add_events(gtk.gdk.KEY_PRESS_MASK)

Did you include the key-press-event in the list of events the widget is allowed to receive? You can do that by calling

browser.add_events(gtk.gdk.KEY_PRESS_MASK)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文