GTK Python MenuButton with GioAction weird behavior


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


class ExampleWindow(Gtk.ApplicationWindow):
    def __init__(self, **kwargs):
        self.set_title(title='Menu Shortcut Bug PoC')
        gio_menu =
        if TRIGGER_BUG:
        popover_menu = Gtk.PopoverMenu.new_from_model(gio_menu)
        headerbar =
        menu_button =

class ExampleApplication(Gtk.Application):
    def __init__(self):
        action ='poc', None)
        action.connect('activate', self.on_poc_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)

    def on_poc_action(self, action, param):
        print('Action activated')

if __name__ == '__main__':
    import sys

    app = ExampleApplication()

CtrlP seems to work fine here?

What I will say though is using append with a kwarg is an odd choice, and using it without an action is nonsensical

Yeah I should have deleted the kwargs aswell for the PoC. Thanks for the hint.

But is the Ctrl + P actually working for you even after you have opened the menu popover via the menu button? Because for me at this point it stops working…

With True or False the accel doesn’t do anything when the menu is open, since it’s trying to find something in the menu — and of course the item isn’t actually associated with the action (and thus not the accell)

With the fixed up append call I see:

And can CtrlP with the menu open

Yes i am aware of that. In the real application i do have your proposed menu item that is associated with the action, but that is not the point i am trying to make.

What i am trying to describe is that after the menu is closed again the shortcut Ctrl+P, that worked initially, before the menu was opened, then stops working. And this shortcut not working is independent of whether the action was bound to the menu or not. The variable TRIGGER_BUG shows that simply opening up a menu does not impact the functioning of the shortcut, only after appending at least one item (that is in no relation to the action as it is not associated) to the menu this behavior is triggered and the shortcut ceases to function.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.