我在将加速器添加到只包含一个Gtk.EventBox()的Vte.Terminal()菜单时遇到了问题。菜单看起来没问题,复制和粘贴都很好,但是加速器似乎不起作用。它们在到达我的eventBox (奇怪的是,因为我的事件箱在VTE小部件之上)之前就被vte抓走了,例如,Ctrl+Shift+C在终端上充当Ctrl+C工作,只是中断了当前的进程。有什么办法可以解决这个问题吗?
菜单项目-加速器的相关关联是注释代码。
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()发布于 2016-06-02 19:00:23
好吧,万一还有人需要帮忙。我放弃了GTK菜单加速器,所以我转而使用我的Vte Widget。在那里,我将key_press_event信号连接到一个检测按下哪些键的方法,万一它找到了返回的Ctrl+Shift的组合。返回是很重要的,因为这就是阻止VTE实际做其他事情的原因,比如在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 Truehttps://stackoverflow.com/questions/36724450
复制相似问题