Hi. I’d like to know if GtkApplication is necessary for creating any kind of Menu?? Because my trivial program , doesn’t work( menu buttons are insensitive). Please give me a good example in Python that uses no GtkApplication.
Additionally, I find use of linking GioAction’s name and Menus, a bit strange. Like instead of providing name to connect to the menu item with conventions like ‘app’ or ‘win’, we can simply use the objects. Something like this :
menuitem = Gio.MenuItem(name=“File”)
action = Gio.Action()
A side note: GtkMenu and related widgets have been removed from GTK4. Application developers should always use GMenu and GAction to describe their menus, especially if they wish to remain forward compatible.
The namespacing is meant to be used when exposing actions over an IPC mechanism, like DBus. This allows calling actions remotely from other components, like the Shell; or to have the menus moved over to the top panel, on macOS. The app vs win namespace is less important, now that GNOME has deprecated the app menu; but in the future, we’re going to expose more namespaces in GTK—like shortcut actions, for instance; or accessibility-related ones; or actions in context menus.
I will give it a try. Not sure how to get around the xml in python. I see there is a GMenu.Tree in the lazka documentation but I don’t know how to create a GMenu without the xml. If I have some things wrong, tell me. Trying to learn these things myself and I am not the greatest at python.
I created a GMenu example with GAction without XML some months ago, because I was not able to find one. Was working, but I got not yet confirmation from people like ebassi or baedert that it really is correct – and well ebassi generally recomments using gtk-builder with XML files, so we should better try using that XML.
Is there a way in this forum for adding a plain link without these ugly preview pictures?
The GMenu.Tree is part of gnome-desktop, and it’s meant to be used to create menus of installed applications; it has nothing to do with application menus. Sadly, there’s a bit of a namespace collision.
A programmatic menu description from the XML you pasted would look like this:
from gi.repository import Gio
# The main menu
menu = Gio.Menu()
# The "open file" menu item
open_item = Gio.MenuItem()
# The "save file" menu item
save_item = Gio.MenuItem()
# The open and save menu items are part of a "section"; sections are used
# to tell the component that will render the menu to visually/semantically
# separate the items
section_menu = Gio.Menu()
# Put the section in a sub-menu, without a label
submenu = Gio.Menu()
# Put the sub-menu in the main menu, with a label