I have created a class called Menu:
class Menu(Gtk.PopoverMenu):
def __init__(self):
Gtk.PopoverMenu.__init__(self)
# Creates a Gio.MenuModel object
menu = Gio.Menu.new()
menu.append('Test 1', 'popup.test1')
menu.append('Test 2', 'popup.test2')
# Takes Gio.MenuModel as it's first parameter
self.bind_model(menu, 'contextMenu')
def show_menu(self):
self.popup()
I instantiate this class from another class as follows:
elif event.type == Gdk.EventType.BUTTON_PRESS and event.button == 3:
popup = Menu()
popup.show_menu()
This executes without any errors, but no menu displays.
I was expecting a popup menu to display on right-click.
The problem was that I needed to use 'set_relative_to(widget)' where 'widget' is provided by a parameter to the button_press_event
. That results in the menu displaying. However, there was a subsequent problem that resulted in menu items being greyed-out despite the fact that they showed as being enabled. To fix that problem, it is necessary that the 2nd parameter of bind_model()
be the same as that used in menu.append()
.
Like this:
self.bind_model(menu, 'popup')