MenuBar (Python) Tiffany Antopolski tiffany.antopolski@gmail.com 2012 Marta Maria Casetti mmcasetti@gmail.com 2012 A widget which holds GtkMenuItem widgets MenuBar created using XML and GtkBuilder

A MenuBar created using XML and GtkBuilder.

Create a MenuBar using XML

To create the menubar using XML:

Create menubar.ui using your favorite text editor.

Enter the following line at the top of the file:

]]>

We want to create the interface which will contain our menubar and its submenus. Our menubar will contain File, Edit, Choices and Help submenus. We add the following XML code to the file:

Now we will create the .py file and use GtkBuilder to import the menubar.ui we just created.

Add the MenuBar to the window using GtkBuilder

Now run the python application. It should look like the picture at the top of this page.

Add items to the menus

We start off by adding 2 menuitems to the File menu: New and Quit. We do this by adding a section to the the File submenu with these items. The menubar.ui should look like this (lines 6 to 13 inclusive comprise the newly added section):

menubar.ui File
New Quit
Edit Choices Help
]]>

Following this pattern, you can now add a Copy and a Paste item to the Edit submenu, and an About item to the Help submenu.

Setup actions

We now create the actions for "New" and "Quit" connected to a callback function in the Python file; for instance we create "new" as:

new_action = Gio.SimpleAction.new("new", None) new_action.connect("activate", self.new_callback)

And we create the callback function of "new" as

def new_callback(self, action, parameter): print "You clicked \"New\""

Now, in the XML file, we connect the menu items to the actions in the XML file by adding the "action" attribute:

New app.new ]]>

Note that for an action that is relative to the application, we use the prefix app.; for actions that are relative to the window we use the prefix win..

Finally, in the Python file, we add the action to the application or to the window - so for instance app.new will be added to the application in the method do_startup(self) as

self.add_action(new_action)

See for a more detailed explanation of signals and callbacks.

Actions: Application or Window?

Above, we created the "new" and "open" actions as part of the MyApplication class. Actions which control the application itself, such as "quit" should be created similarly.

Some actions, such as "copy" and "paste" deal with the window, not the application. Window actions should be created as part of the window class.

The complete example files contain both application actions and window actions. The window actions are the ones usually included in the application menu also. It is not good practice to include window actions in the application menu. For demonstration purposes, the complete example files which follow include XML in the UI file which creates the application menu which includes a "New" and "Open" item, and these are hooked up to the same actions as the menubar items of the same name.

Choices submenu and items with state

Lines 30 to 80 inclusive of the demonstrate the XML code used to create the UI for Choices menu.

The actions created so far are stateless, that is they do not retain or depend on a state given by the action itself. The actions we need to create for the Choices submenu, on the other hand, are stateful. An example of creation of a stateful action is:

shape_action = Gio.SimpleAction.new_stateful("shape", GLib.VariantType.new('s'), GLib.Variant.new_string('line'))

where the variables of the method are: name, parameter type (in this case, a string - see here for a complete list of character meanings), initial state (in this case, 'line' - in case of a True boolean value it should be Glib.Variant.new_boolean(True), and so on, see here for a complete list)

After creating the stateful SimpleAction we connect it to the callback function and we add it to the window (or the application, if it is the case), as before:

shape_action.connect("activate", self.shape_callback) self.add_action(shape_action)
Complete XML UI file for this example
Complete Python file for this example
Mnemonics and Accelerators

Labels may contain mnemonics. Mnemonics are underlined characters in the label, used for keyboard navigation. Mnemonics are created by placing an underscore before the mnemonic character. For example "_File" instead of just "File" in the menubar.ui label attribute.

The mnemonics are visible when you press the Alt key. Pressing AltF will open the File menu.

Accelerators can be explicitly added in the UI definitions. For example, it is common to be able to quit an application by pressing CtrlQ or to save a file by pressing CtrlS. To add an accelerator to the UI definition, you simply need add an "accel" attribute to the item.

<Primary>q]]> will create the CtrlQ sequence when added to the Quit label item. Here, "Primary" refers to the Ctrl key on a PC or the key on a Mac.

_Quit app.quit <Primary>q ]]>
Translatable strings

Since GNOME applications are being translated into many languages, it is important that the strings in your application are translatable. To make a label translatable, simple set translatable="yes":

Quit]]>
API References

In this sample we used the following:

GSimpleAction

GtkBuilder