Python GTK+;:将自定义加速器添加到VTE
向仅包含Vte.Terminal()的Gtk.EventBox()上显示的菜单添加加速器时遇到问题。菜单显示正常,复制和粘贴工作正常,但加速器似乎不起作用。它们在到达我的eventBox之前被VTE抓取(奇怪的是,因为我的eventBox在VTE小部件的上方),例如,Ctrl+Shift+C在终端上充当Ctrl+C并中断当前进程。你有什么想法吗 menuitem accelerator的相关关联是注释代码Python GTK+;:将自定义加速器添加到VTE,python,gtk,Python,Gtk,向仅包含Vte.Terminal()的Gtk.EventBox()上显示的菜单添加加速器时遇到问题。菜单显示正常,复制和粘贴工作正常,但加速器似乎不起作用。它们在到达我的eventBox之前被VTE抓取(奇怪的是,因为我的eventBox在VTE小部件的上方),例如,Ctrl+Shift+C在终端上充当Ctrl+C并中断当前进程。你有什么想法吗 menuitem accelerator的相关关联是注释代码 def terminalBox(self, terminal): """Given
def terminalBox(self, terminal):
"""Given a terminal, creates an EventBox for the Box that has as
a children said terminal"""
eventTerminalBox = Gtk.EventBox()
terminalBox = Gtk.Box()
terminalBox.pack_start(terminal, True, True, 0)
eventTerminalBox.connect("button_press_event", self.right_click)
eventTerminalBox.add(terminalBox)
return eventTerminalBox
def right_click(self, eventbox, event):
"""Defines the menu created when a user rightclicks on the
terminal eventbox"""
menu = Gtk.Menu()
copy = Gtk.MenuItem("Copy")
paste = Gtk.MenuItem("Paste")
menu.append(paste)
menu.append(copy)
# TODO: make accelerators for copy paste work. add accel for paste
#accelgroup = Gtk.AccelGroup()
#self.add_accel_group(accelgroup)
#accellabel = Gtk.AccelLabel("Copy/Paste")
#accellabel.set_hexpand(True)
#copy.add_accelerator("activate",
# accelgroup,
# Gdk.keyval_from_name("c"),
# Gdk.ModifierType.SHIFT_MASK |
# Gdk.ModifierType.CONTROL_MASK,
# Gtk.AccelFlags.VISIBLE)
copy.connect("activate", self.copy_text)
paste.connect("activate", self.paste_text)
copy.show()
paste.show()
menu.popup(None, None, None, None, event.button, event.time)
def copy_text(self, button):
"""What happens when the user copies text"""
content = self.selection_clipboard.wait_for_text()
self.clipboard.set_text(content, -1)
def paste_text(self, button):
"""What happens when the user pastes text"""
currentTerminal = self.getCurrentFocusedTerminal()
currentTerminal.paste_clipboard()
好的,以防其他人需要帮助。我放弃了使用GTK菜单加速器,所以转而使用我的Vte小部件。在那里,我将按键按下事件信号连接到一个方法,该方法检测按下了哪些键,如果它找到Ctrl+Shift组合,它将返回。返回很重要,因为这是阻止VTE使用shorcut实际执行其他操作的原因,例如在Ctrl+Shift+C上终止进程
class Terminal(Vte.Terminal):
"""Defines a simple terminal"""
def __init__(self, CONF):
super(Vte.Terminal, self).__init__()
self.pty = self.pty_new_sync(Vte.PtyFlags.DEFAULT, None)
self.set_pty(self.pty)
self.connect("key_press_event", self.copy_or_paste)
self.set_scrollback_lines(-1)
self.set_audible_bell(0)
def copy_or_paste(self, widget, event):
"""Decides if the Ctrl+Shift is pressed, in which case returns True.
If Ctrl+Shift+C or Ctrl+Shift+V are pressed, copies or pastes,
acordingly. Return necesary so it doesn't perform other action,
like killing the process, on Ctrl+C.
"""
control_key = Gdk.ModifierType.CONTROL_MASK
shift_key = Gdk.ModifierType.SHIFT_MASK
if event.type == Gdk.EventType.KEY_PRESS:
if event.state == shift_key | control_key: #both shift and control
if event.keyval == 67: # that's the C key
self.copy_clipboard()
elif event.keyval == 86: # and that's the V key
self.paste_clipboard()
return True