Gtk.EntryCompletion()弹出状态来解决keybind冲突

时间:2016-06-20 08:47:05

标签: python python-3.x gtk gtk3 pygobject

我有 Gtk.Entry() Gtk.EntryCompletion(),绑定了箭头键以执行不同的功能,即在终端中导航命令历史记录

但是完成弹出窗口上的导航会与历史导航冲突,有没有办法检查完成弹出窗口是否可见/活动,以便我们可以关闭历史记录导航

另外,是否可以获得完成弹出窗口的总匹配数。

以下是示例程序。

#!/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()

1 个答案:

答案 0 :(得分:1)

您需要使用True将内联选择选项设置为entrycompletion.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()