|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
xmlns:its="http://www.w3.org/2005/11/its"
|
|
Packit |
1470ea |
xmlns:xi="http://www.w3.org/2001/XInclude"
|
|
Packit |
1470ea |
type="guide" style="task"
|
|
Packit |
1470ea |
id="menubar.py">
|
|
Packit |
1470ea |
<info>
|
|
Packit |
1470ea |
<title type="text">MenuBar (Python)</title>
|
|
Packit |
1470ea |
<link type="guide" xref="beginner.py#menu-combo-toolbar"/>
|
|
Packit |
1470ea |
<link type="seealso" xref="gmenu.py"/>
|
|
Packit |
1470ea |
<link type="next" xref="colorbutton.py"/>
|
|
Packit |
1470ea |
<revision version="0.1" date="2012-08-01" status="stub"/>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<credit type="author copyright">
|
|
Packit |
1470ea |
<name>Tiffany Antopolski</name>
|
|
Packit |
1470ea |
<email its:translate="no">tiffany.antopolski@gmail.com</email>
|
|
Packit |
1470ea |
<years>2012</years>
|
|
Packit |
1470ea |
</credit>
|
|
Packit |
1470ea |
<credit type="author copyright">
|
|
Packit |
1470ea |
<name>Marta Maria Casetti</name>
|
|
Packit |
1470ea |
<email its:translate="no">mmcasetti@gmail.com</email>
|
|
Packit |
1470ea |
<years>2012</years>
|
|
Packit |
1470ea |
</credit>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<desc>A widget which holds GtkMenuItem widgets</desc>
|
|
Packit |
1470ea |
</info>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<title>MenuBar created using XML and GtkBuilder</title>
|
|
Packit |
1470ea |
<media type="image" mime="image/png" src="media/menubar.png"/>
|
|
Packit |
1470ea |
A MenuBar created using XML and GtkBuilder.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<links type="section"/>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="xml"> <title>Create a MenuBar using XML</title>
|
|
Packit |
1470ea |
To create the menubar using XML:
|
|
Packit |
1470ea |
<steps>
|
|
Packit |
1470ea |
<item>Create <file>menubar.ui</file> using your favorite text editor. </item>
|
|
Packit |
1470ea |
<item>Enter the following line at the top of the file:
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
]]>
|
|
Packit |
1470ea |
</item>
|
|
Packit |
1470ea |
<item>We want to create the interface which will contain our menubar and its submenus. Our menubar will contain <gui>File</gui>, <gui>Edit</gui>, <gui>Choices</gui> and <gui>Help</gui> submenus. We add the following XML code to the file:
|
|
Packit |
1470ea |
<xi:include href="samples/menubar_basis.ui" parse="text"><xi:fallback/></xi:include>
|
|
Packit |
1470ea |
</item>
|
|
Packit |
1470ea |
<item>Now we will create the .py file and use GtkBuilder to import the <file>menubar.ui</file> we just created. </item>
|
|
Packit |
1470ea |
</steps>
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="basis"> <title>Add the MenuBar to the window using GtkBuilder</title>
|
|
Packit |
1470ea |
<xi:include href="samples/menubar_basis.py" parse="text"><xi:fallback/></xi:include>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
Now run the python application. It should look like the picture at the top of this page.
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="xml2"> <title>Add items to the menus</title>
|
|
Packit |
1470ea |
We start off by adding 2 menuitems to the <gui>File</gui> menu: <gui>New</gui> and <gui>Quit</gui>. We do this by adding a section to the the File submenu with these items. The <file>menubar.ui</file> should look like this (lines 6 to 13 inclusive comprise the newly added section):
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<listing>
|
|
Packit |
1470ea |
<title>menubar.ui</title>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<interface>
|
|
Packit |
1470ea |
<menu id="menubar">
|
|
Packit |
1470ea |
<submenu>
|
|
Packit |
1470ea |
<attribute name="label">File</attribute>
|
|
Packit |
1470ea |
<section>
|
|
Packit |
1470ea |
<item>
|
|
Packit |
1470ea |
<attribute name="label">New</attribute>
|
|
Packit |
1470ea |
</item>
|
|
Packit |
1470ea |
<item>
|
|
Packit |
1470ea |
<attribute name ="label">Quit</attribute>
|
|
Packit |
1470ea |
</item>
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
</submenu>
|
|
Packit |
1470ea |
<submenu>
|
|
Packit |
1470ea |
<attribute name="label">Edit</attribute>
|
|
Packit |
1470ea |
</submenu>
|
|
Packit |
1470ea |
<submenu>
|
|
Packit |
1470ea |
<attribute name="label">Choices</attribute>
|
|
Packit |
1470ea |
</submenu>
|
|
Packit |
1470ea |
<submenu>
|
|
Packit |
1470ea |
<attribute name="label">Help</attribute>
|
|
Packit |
1470ea |
</submenu>
|
|
Packit |
1470ea |
</menu>
|
|
Packit |
1470ea |
</interface>]]>
|
|
Packit |
1470ea |
</listing>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
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.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="actions">
|
|
Packit |
1470ea |
<title>Setup actions</title>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
We now create the actions for "New" and "Quit" connected to a callback function in the Python file; for instance we create "new" as:
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
new_action = Gio.SimpleAction.new("new", None)
|
|
Packit |
1470ea |
new_action.connect("activate", self.new_callback)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
And we create the callback function of "new" as
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
def new_callback(self, action, parameter):
|
|
Packit |
1470ea |
print "You clicked \"New\""
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
Now, in the XML file, we connect the menu items to the actions in the XML file by adding the "action" attribute:
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<item>
|
|
Packit |
1470ea |
<attribute name="label">New</attribute>
|
|
Packit |
1470ea |
<attribute name="action">app.new</attribute>
|
|
Packit |
1470ea |
</item>]]>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
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. .
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
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
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
self.add_action(new_action)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
See <link xref="signals-callbacks.py"/> for a more detailed explanation of signals and callbacks.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="win-app"><title>Actions: Application or Window?</title>
|
|
Packit |
1470ea |
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.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
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.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
The complete example files contain both application actions and window actions. The window actions are the ones usually included in the <link xref="gmenu.py">application menu</link> 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.
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="choices">
|
|
Packit |
1470ea |
<title>Choices submenu and items with state</title>
|
|
Packit |
1470ea |
<media type="image" mime="image/png" src="media/menubar_choices.png"/>
|
|
Packit |
1470ea |
Lines 30 to 80 inclusive of the <link xref="menubar.py#xml-code" /> demonstrate the XML code used to create the UI for <gui>Choices</gui> menu.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
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:
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
shape_action = Gio.SimpleAction.new_stateful("shape", GLib.VariantType.new('s'), GLib.Variant.new_string('line'))
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
where the variables of the method are: name, parameter type (in this case, a string - see <link href="http://developer.gnome.org/glib/unstable/glib-GVariantType.html">here</link> 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 <link href="http://developer.gnome.org/glib/unstable/glib-GVariant.html">here</link> for a complete list)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
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:
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
shape_action.connect("activate", self.shape_callback)
|
|
Packit |
1470ea |
self.add_action(shape_action)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="xml-code">
|
|
Packit |
1470ea |
<title>Complete XML UI file for this example</title>
|
|
Packit |
1470ea |
<xi:include href="samples/menubar.ui" parse="text"><xi:fallback/></xi:include>
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="python-code">
|
|
Packit |
1470ea |
<title>Complete Python file for this example</title>
|
|
Packit |
1470ea |
<xi:include href="samples/menubar.py" parse="text"><xi:fallback/></xi:include>
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="mnemonics-and-accelerators"><title>Mnemonics and Accelerators</title>
|
|
Packit |
1470ea |
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.
|
|
Packit |
1470ea |
The mnemonics are visible when you press the <key>Alt</key> key. Pressing <keyseq><key>Alt</key><key>F</key></keyseq> will open the <gui>File</gui> menu.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
Accelerators can be explicitly added in the UI definitions. For example, it is common to be able to quit an application by pressing <keyseq><key>Ctrl</key><key>Q</key></keyseq> or to save a file by pressing <keyseq><key>Ctrl</key><key>S</key></keyseq>. To add an accelerator to the UI definition, you simply need add an "accel" attribute to the item.
|
|
Packit |
1470ea |
<Primary>q</attribute>]]> will create the <keyseq><key>Ctrl</key><key>Q</key></keyseq> sequence when added to the Quit label item. Here, "Primary" refers to the <key>Ctrl</key> key on a PC or the <key>⌘</key> key on a Mac.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<item>
|
|
Packit |
1470ea |
<attribute name="label">_Quit</attribute>
|
|
Packit |
1470ea |
<attribute name="action">app.quit</attribute>
|
|
Packit |
1470ea |
<attribute name="accel"><Primary>q</attribute>
|
|
Packit |
1470ea |
</item>]]>
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="translatable"><title>Translatable strings</title>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
Since GNOME applications are being translated into <link href="http://l10n.gnome.org/languages/">many languages</link>, it is important that the strings in your application are translatable. To make a label translatable, simple set translatable="yes" :
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
Quit</attribute>]]>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
<section id="references">
|
|
Packit |
1470ea |
<title>API References</title>
|
|
Packit |
1470ea |
In this sample we used the following:
|
|
Packit |
1470ea |
<list>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gio/unstable/GSimpleAction.html">GSimpleAction</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/unstable/GtkBuilder.html">GtkBuilder</link> </item>
|
|
Packit |
1470ea |
</list>
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
</page>
|