Hello,
I just now discovered some weird behavior during the development of an application with GTK4 and Python. I have reduced the problem to this minimal PoC. In essence when pressing the shortcut Ctrl
+P
a Gio.Action is triggered. But as soon as the Menu Popover with at least one item is created, this shortcut stops working.
As I am new to GTK development, it can be, that i used the Gio.Actions wrong, and this is behavior is expected. If so, i would be interested why this is the case and maybe a resource or short example, of how it would be done correctly.
If it is unintended behavior it would be interesting to reproduce this potential bug with c, to see whether this is part of the python bindings or in GTK itself.
# -*- coding: utf-8 -*-
import gi
gi.require_version(namespace='Gtk', version='4.0')
from gi.repository import Gio, Gtk
TRIGGER_BUG = True
class ExampleWindow(Gtk.ApplicationWindow):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.set_title(title='Menu Shortcut Bug PoC')
gio_menu = Gio.Menu.new()
if TRIGGER_BUG:
gio_menu.append(label='test')
popover_menu = Gtk.PopoverMenu.new_from_model(gio_menu)
headerbar = Gtk.HeaderBar.new()
self.set_titlebar(titlebar=headerbar)
menu_button = Gtk.MenuButton.new()
menu_button.set_popover(popover=popover_menu)
headerbar.pack_end(child=menu_button)
class ExampleApplication(Gtk.Application):
def __init__(self):
super().__init__(application_id='org.poc')
action = Gio.SimpleAction.new('poc', None)
action.connect('activate', self.on_poc_action)
self.add_action(action)
self.set_accels_for_action(f'app.poc', ['<primary>p'])
def do_activate(self):
win = self.props.active_window
if not win:
win = ExampleWindow(application=self)
win.present()
def on_poc_action(self, action, param):
print('Action activated')
if __name__ == '__main__':
import sys
app = ExampleApplication()
app.run(sys.argv)