3

This should be a no-brainer but I just can't seem to figure it out. If I build a Gtk.PopoverMenu:

menu = Gio.Menu.new()
test = Gio.MenuItem.new(label='test', detailed_action='win.test')
menu.append_item(test)
popup = Gtk.PopoverMenu.new_from_model(menu)

I don't know how to attach it to the parent widget (in my case a ListBox item). The API docs say "Popovers are attached to a parent widget" but there seems to be no relevant method for attaching it to the parent. Trying to popup.popup() results in a warning "Calling gtk_widget_realize() on a widget that isn't inside a toplevel window is not going to work very well. Widgets must be inside a toplevel container before realizing them", and a subsequent segfault.

Andrej Prsa
  • 551
  • 3
  • 14
  • 1
    In Gtk3, you can set the parent like this: `popup.set_parent(parent_widget)`. Or did you already try that? What counts as a "relevant method"? – Sylvester Kruin Feb 21 '22 at 17:16
  • 1
    Holy smokes, it worked! I tried sooooo many combinations but that one eluded me. Can you post this as a response so that I can upvote it? – Andrej Prsa Feb 21 '22 at 17:31
  • 1
    Glad to help! It's sad how much trouble lack-of-documentation can cause. In fact, I didn't actually use documentation to figure that one out; I used Python's `help()` built-in function. That way, it's sure to cover everything. Just a tip you may want to keep in mind :-). – Sylvester Kruin Feb 21 '22 at 17:34

1 Answers1

2

To set the parent of the PopoverMenu, just use its set_parent() method to set the parent to any widget:

...
popup = Gtk.PopoverMenu.new_from_model(menu)
popup.set_parent(parent_widget)
Sylvester Kruin
  • 3,294
  • 5
  • 16
  • 39