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()
menuitem.add_action(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.
First of all: no, we should have examples in multiple languages in the GNOME documentation, because people use multiple languages to write GNOME applications.
Second: what website had different languages? I only linked two—both on the GNOME wiki. The other link was to the Python/GTK tutorial, which is hosted on a separate infrastructure.
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()
open_item.set_label("Open File")
open_item.set_detailed_action("app.open_file")
# The "save file" menu item
save_item = Gio.MenuItem()
save_item.set_label("Save File")
save_item.set_detailed_action("app.save_file")
# 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()
section_menu.append_item(open_item)
section_menu.append_item(save_item)
# Put the section in a sub-menu, without a label
submenu = Gio.Menu()
submenu.append_section(None, section_menu)
# Put the sub-menu in the main menu, with a label
menu.append_submenu("File", submenu)