import gi gi.require_version("Gtk", "3.0") from gi.repository import Gtk def on_button_clicked(widget): print("Button clicked!") accel_group = Gtk.AccelGroup() window = Gtk.Window() window.connect("destroy", Gtk.main_quit) button = Gtk.Button(label="Click me") button.add_accelerator("clicked", accel_group, ord("c"), Gdk.ModifierType.CONTROL_MASK, Gtk.AccelFlags.VISIBLE) button.connect("clicked", on_button_clicked) window.add(button) window.show_all() Gtk.main()
import gi gi.require_version("Gtk", "3.0") from gi.repository import Gtk def on_key_pressed(widget, event): if event.keyval in [Gdk.KEY_a, Gdk.KEY_A]: print("You pressed the 'a' key!") accel_group = Gtk.AccelGroup() window = Gtk.Window() window.connect("destroy", Gtk.main_quit) button = Gtk.Button(label="Click me") window.add(button) button.add_accel_group(accel_group) button.grab_focus() button.connect("key-press-event", on_key_pressed) window.show_all() Gtk.main()This example creates a Gtk window and a button. It then creates an AccelGroup instance and adds it to the button. The button is given focus, and when any key is pressed while the button is in focus, it will trigger the on_key_pressed() function. The function checks if the key pressed is the 'a' or 'A' key, and if so, it will print "You pressed the 'a' key!" to the console.