Gtk.EntryCompletion() 弹窗状态解决按键绑定冲突

Gtk.EntryCompletion() popup status to resolve keybind conflict

我有 Gtk.Entry()Gtk.EntryCompletion(),绑定了方向键执行不同的功能,即像在终端中一样浏览命令历史记录。

但是 Completion Popup 上的导航会与历史导航冲突,有什么方法可以检查 Completion Popup 是否是 visible/active,这样我们就可以关闭历史导航了。

另外,有没有办法获得 Completion Popup.

的总匹配数

下面是示例程序。

#!/usr/bin/env python3

import gi
gi.require_version('Gtk', '3.0')

from gi.repository import Gtk

entry = Gtk.Entry()

def on_key_press(widget, event):
    # NOTE to stop propagation of signal return True
    if   event.keyval == 65362: widget.set_text("history -1") # Up-arrow
    elif event.keyval == 65364: widget.set_text("history +1") # Down-arrow

entry.connect('key_press_event', on_key_press)

entrycompletion = Gtk.EntryCompletion()
entry.set_completion(entrycompletion)

liststore = Gtk.ListStore(str)
for row in "entry complete key conflit with history".split():
    liststore.append([row])

entrycompletion.set_model(liststore)
entrycompletion.set_text_column(0)

root = Gtk.Window()
root.add(entry)
root.connect('delete-event', Gtk.main_quit)
root.show_all()
Gtk.main()

您需要将内联选择选项设置为 Trueentrycompletion.set_inline_selection(True)。这会将按键事件连接到完成弹出菜单。

#!/usr/bin/env python3

import gi
gi.require_version('Gtk', '3.0')

from gi.repository import Gtk

entry = Gtk.Entry()

def on_key_press(widget, event):
    # NOTE to stop propagation of signal return True
    if   event.keyval == 65362: widget.set_text("history -1") # Up-arrow
    elif event.keyval == 65364: widget.set_text("history +1") # Down-arrow

entry.connect('key_press_event', on_key_press)

entrycompletion = Gtk.EntryCompletion()
entrycompletion.set_inline_selection(True) #enables selection with up and down arrows
entry.set_completion(entrycompletion)

liststore = Gtk.ListStore(str)
for row in "entry complete key conflit with history".split():
    liststore.append([row])

entrycompletion.set_model(liststore)
entrycompletion.set_text_column(0)

root = Gtk.Window()
root.add(entry)
root.connect('delete-event', Gtk.main_quit)
root.show_all()
Gtk.main()