BaRud
BaRud

Reputation: 3218

add keyboard shortcuts to GIo.Menu

I am trying to add accelgroup (keyboard shortcuts) to the Gio.Menu items. But I have not found a way to do this. Like, in this example, I want to open file with <Ctrl>+o

filemenu = Gio.Menu()
filemenu.append("Open","win.open")

accelgroup=Gtk.AccelGroup()
self.add_accel_group(accelgroup)

# Open menu
open_action = Gio.SimpleAction(name="open")
# this line is problematic
open_action.add_accelerator("activate". Gdk._keyval_from_name("O"))
open_action.connect("activate", self.MenuElem.file_open_clicked)
self.add_action(open_action)

How can I do this?

Upvotes: 2

Views: 236

Answers (1)

TingPing
TingPing

Reputation: 2302

You don't add keybindings to the Gio.Action itself you add them to a Widget or Application for example:

app = # My Gtk.Application instance
window = # My Gtk.ApplicationWindow instance
action = Gio.SimpleAction.new('open', None)

window.add_action(action)
app.add_accelerator('<Primary>o', 'win.open', None)
# The 'win.' prefix is because it was added to a Gtk.ApplicationWindow

Upvotes: 3

Related Questions