/usr/share/help/gl/gnome-devel-demos/menubar.py.page is in gnome-devel-docs 3.28.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 | <?xml version="1.0" encoding="utf-8"?>
<page xmlns="http://projectmallard.org/1.0/" xmlns:its="http://www.w3.org/2005/11/its" xmlns:xi="http://www.w3.org/2001/XInclude" type="guide" style="task" id="menubar.py" xml:lang="gl">
<info>
<title type="text">MenuBar (Python)</title>
<link type="guide" xref="beginner.py#menu-combo-toolbar"/>
<link type="seealso" xref="gmenu.py"/>
<link type="next" xref="colorbutton.py"/>
<revision version="0.1" date="2012-08-01" status="stub"/>
<credit type="author copyright">
<name>Tiffany Antopolski</name>
<email its:translate="no">tiffany.antopolski@gmail.com</email>
<years>2012</years>
</credit>
<credit type="author copyright">
<name>Marta Maria Casetti</name>
<email its:translate="no">mmcasetti@gmail.com</email>
<years>2012</years>
</credit>
<desc>A widget which holds GtkMenuItem widgets</desc>
<mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
<mal:name>Fran Dieguez</mal:name>
<mal:email>frandieguez@gnome.org</mal:email>
<mal:years>2012-2013.</mal:years>
</mal:credit>
</info>
<title>MenuBar created using XML and GtkBuilder</title>
<media type="image" mime="image/png" src="media/menubar.png"/>
<p>A MenuBar created using XML and GtkBuilder.</p>
<links type="section"/>
<section id="xml"> <title>Create a MenuBar using XML</title>
<p>To create the menubar using XML:</p>
<steps>
<item><p>Create <file>menubar.ui</file> using your favorite text editor.</p></item>
<item><p>Enter the following line at the top of the file:</p>
<code mime="application/xml"><![CDATA[
<?xml version="1.0"? encoding="UTF-8"?>]]></code>
</item>
<item><p>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:</p>
<code mime="application/xml"><?xml version="1.0" encoding="UTF-8"?>
<interface>
<menu id="menubar">
<submenu>
<attribute name="label">File</attribute>
</submenu>
<submenu>
<attribute name="label">Edit</attribute>
</submenu>
<submenu>
<attribute name="label">Choices</attribute>
</submenu>
<submenu>
<attribute name="label">Help</attribute>
</submenu>
</menu>
</interface>
</code>
</item>
<item><p>Now we will create the .py file and use GtkBuilder to import the <file>menubar.ui</file> we just created.</p></item>
</steps>
</section>
<section id="basis"> <title>Add the MenuBar to the window using GtkBuilder</title>
<code mime="text/x-python">from gi.repository import Gtk
import sys
class MyWindow(Gtk.ApplicationWindow):
def __init__(self, app):
Gtk.Window.__init__(self, title="MenuBar Example", application=app)
self.set_default_size(200, 200)
class MyApplication(Gtk.Application):
def __init__(self):
Gtk.Application.__init__(self)
def do_activate(self):
win = MyWindow(self)
win.show_all()
def do_startup(self):
Gtk.Application.do_startup(self)
# a builder to add the UI designed with Glade to the grid:
builder = Gtk.Builder()
# get the file (if it is there)
try:
builder.add_from_file("menubar_basis.ui")
except:
print("file not found")
sys.exit()
# we use the method Gtk.Application.set_menubar(menubar) to add the menubar
# to the application (Note: NOT the window!)
self.set_menubar(builder.get_object("menubar"))
app = MyApplication()
exit_status = app.run(sys.argv)
sys.exit(exit_status)
</code>
<p>
Now run the python application. It should look like the picture at the top of this page.</p>
</section>
<section id="xml2"> <title>Add items to the menus</title>
<p>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 <code>section</code> to the the <code>File</code> submenu with these items. The <file>menubar.ui</file> should look like this (lines 6 to 13 inclusive comprise the newly added section):</p>
<listing>
<title>menubar.ui</title>
<code mime="application/xml" style="numbered"><![CDATA[
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<menu id="menubar">
<submenu>
<attribute name="label">File</attribute>
<section>
<item>
<attribute name="label">New</attribute>
</item>
<item>
<attribute name ="label">Quit</attribute>
</item>
</section>
</submenu>
<submenu>
<attribute name="label">Edit</attribute>
</submenu>
<submenu>
<attribute name="label">Choices</attribute>
</submenu>
<submenu>
<attribute name="label">Help</attribute>
</submenu>
</menu>
</interface>]]></code>
</listing>
<p>Following this pattern, you can now add a <code>Copy</code> and a <code>Paste</code> item to the <code>Edit</code> submenu, and an <code>About</code> item to the <code>Help</code> submenu.</p>
</section>
<section id="actions">
<title>Setup actions</title>
<p>We now create the actions for "New" and "Quit" connected to a callback function in the Python file; for instance we create "new" as:</p>
<code mime="text/x-python">
new_action = Gio.SimpleAction.new("new", None)
new_action.connect("activate", self.new_callback)</code>
<p>And we create the callback function of "new" as</p>
<code mime="text/x-python">
def new_callback(self, action, parameter):
print "You clicked \"New\""</code>
<p>Now, in the XML file, we connect the menu items to the actions in the XML file by adding the "action" attribute:</p>
<code mime="text/x-python"><![CDATA[
<item>
<attribute name="label">New</attribute>
<attribute name="action">app.new</attribute>
</item>]]></code>
<p>Note that for an action that is relative to the application, we use the prefix <code>app.</code>; for actions that are relative to the window we use the prefix <code>win.</code>.</p>
<p>Finally, in the Python file, we add the action to the application or to the window - so for instance <code>app.new</code> will be added to the application in the method <code>do_startup(self)</code> as</p>
<code mime="text/x-python">
self.add_action(new_action)</code>
<p>See <link xref="signals-callbacks.py"/> for a more detailed explanation of signals and callbacks.</p>
</section>
<section id="win-app"><title>Actions: Application or Window?</title>
<p>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.</p>
<p>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.</p>
<p>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.</p>
</section>
<section id="choices">
<title>Choices submenu and items with state</title>
<media type="image" mime="image/png" src="media/menubar_choices.png"/>
<p>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.</p>
<p>The actions created so far are <em>stateless</em>, 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 <em>stateful</em>. An example of creation of a stateful action is:</p>
<code mime="text/x-python">
shape_action = Gio.SimpleAction.new_stateful("shape", GLib.VariantType.new('s'), GLib.Variant.new_string('line'))</code>
<p>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 <code>True</code> boolean value it should be <code>Glib.Variant.new_boolean(True)</code>, and so on, see <link href="http://developer.gnome.org/glib/unstable/glib-GVariant.html">here</link> for a complete list)</p>
<p>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:</p>
<code mime="text/x-python">
shape_action.connect("activate", self.shape_callback)
self.add_action(shape_action)</code>
</section>
<section id="xml-code">
<title>Complete XML UI file for this example</title>
<code mime="application/xml" style="numbered"><?xml version="1.0" encoding="UTF-8"?>
<interface>
<menu id="menubar">
<submenu>
<attribute name="label">File</attribute>
<section>
<item>
<attribute name="label">New</attribute>
<attribute name="action">app.new</attribute>
</item>
<item>
<attribute name="label">Quit</attribute>
<attribute name="action">app.quit</attribute>
</item>
</section>
</submenu>
<submenu>
<attribute name="label">Edit</attribute>
<section>
<item>
<attribute name="label">Copy</attribute>
<attribute name="action">win.copy</attribute>
</item>
<item>
<attribute name="label">Paste</attribute>
<attribute name="action">win.paste</attribute>
</item>
</section>
</submenu>
<submenu>
<attribute name="label">Choices</attribute>
<submenu>
<attribute name="label">Shapes</attribute>
<section>
<item>
<attribute name="label">Line</attribute>
<attribute name="action">win.shape</attribute>
<attribute name="target">line</attribute>
</item>
<item>
<attribute name="label">Triangle</attribute>
<attribute name="action">win.shape</attribute>
<attribute name="target">triangle</attribute>
</item>
<item>
<attribute name="label">Square</attribute>
<attribute name="action">win.shape</attribute>
<attribute name="target">square</attribute>
</item>
<item>
<attribute name="label">Polygon</attribute>
<attribute name="action">win.shape</attribute>
<attribute name="target">polygon</attribute>
</item>
<item>
<attribute name="label">Circle</attribute>
<attribute name="action">win.shape</attribute>
<attribute name="target">circle</attribute>
</item>
</section>
</submenu>
<section>
<item>
<attribute name="label">On</attribute>
<attribute name="action">app.state</attribute>
<attribute name="target">on</attribute>
</item>
<item>
<attribute name="label">Off</attribute>
<attribute name="action">app.state</attribute>
<attribute name="target">off</attribute>
</item>
</section>
<section>
<item>
<attribute name="label">Awesome</attribute>
<attribute name="action">app.awesome</attribute>
</item>
</section>
</submenu>
<submenu>
<attribute name="label">Help</attribute>
<section>
<item>
<attribute name="label">About</attribute>
<attribute name="action">win.about</attribute>
</item>
</section>
</submenu>
</menu>
<menu id="appmenu">
<section>
<item>
<attribute name="label">New</attribute>
<attribute name="action">app.new</attribute>
</item>
<item>
<attribute name="label">Quit</attribute>
<attribute name="action">app.quit</attribute>
</item>
</section>
</menu>
</interface>
</code>
</section>
<section id="python-code">
<title>Complete Python file for this example</title>
<code mime="text/x-python" style="numbered">from gi.repository import Gtk
from gi.repository import GLib
from gi.repository import Gio
import sys
class MyWindow(Gtk.ApplicationWindow):
def __init__(self, app):
Gtk.Window.__init__(self, title="MenuBar Example", application=app)
self.set_default_size(200, 200)
# action without a state created (name, parameter type)
copy_action = Gio.SimpleAction.new("copy", None)
# connected with the callback function
copy_action.connect("activate", self.copy_callback)
# added to the window
self.add_action(copy_action)
# action without a state created (name, parameter type)
paste_action = Gio.SimpleAction.new("paste", None)
# connected with the callback function
paste_action.connect("activate", self.paste_callback)
# added to the window
self.add_action(paste_action)
# action with a state created (name, parameter type, initial state)
shape_action = Gio.SimpleAction.new_stateful(
"shape", GLib.VariantType.new('s'), GLib.Variant.new_string('line'))
# connected to the callback function
shape_action.connect("activate", self.shape_callback)
# added to the window
self.add_action(shape_action)
# action with a state created
about_action = Gio.SimpleAction.new("about", None)
# action connected to the callback function
about_action.connect("activate", self.about_callback)
# action added to the application
self.add_action(about_action)
# callback function for copy_action
def copy_callback(self, action, parameter):
print("\"Copy\" activated")
# callback function for paste_action
def paste_callback(self, action, parameter):
print("\"Paste\" activated")
# callback function for shape_action
def shape_callback(self, action, parameter):
print("Shape is set to", parameter.get_string())
# Note that we set the state of the action!
action.set_state(parameter)
# callback function for about (see the AboutDialog example)
def about_callback(self, action, parameter):
# a Gtk.AboutDialog
aboutdialog = Gtk.AboutDialog()
# lists of authors and documenters (will be used later)
authors = ["GNOME Documentation Team"]
documenters = ["GNOME Documentation Team"]
# we fill in the aboutdialog
aboutdialog.set_program_name("MenuBar Example")
aboutdialog.set_copyright(
"Copyright \xc2\xa9 2012 GNOME Documentation Team")
aboutdialog.set_authors(authors)
aboutdialog.set_documenters(documenters)
aboutdialog.set_website("http://developer.gnome.org")
aboutdialog.set_website_label("GNOME Developer Website")
# to close the aboutdialog when "close" is clicked we connect the
# "response" signal to on_close
aboutdialog.connect("response", self.on_close)
# show the aboutdialog
aboutdialog.show()
# a callback function to destroy the aboutdialog
def on_close(self, action, parameter):
action.destroy()
class MyApplication(Gtk.Application):
def __init__(self):
Gtk.Application.__init__(self)
def do_activate(self):
win = MyWindow(self)
win.show_all()
def do_startup(self):
# FIRST THING TO DO: do_startup()
Gtk.Application.do_startup(self)
# action without a state created
new_action = Gio.SimpleAction.new("new", None)
# action connected to the callback function
new_action.connect("activate", self.new_callback)
# action added to the application
self.add_action(new_action)
# action without a state created
quit_action = Gio.SimpleAction.new("quit", None)
# action connected to the callback function
quit_action.connect("activate", self.quit_callback)
# action added to the application
self.add_action(quit_action)
# action with a state created
state_action = Gio.SimpleAction.new_stateful(
"state", GLib.VariantType.new('s'), GLib.Variant.new_string('off'))
# action connected to the callback function
state_action.connect("activate", self.state_callback)
# action added to the application
self.add_action(state_action)
# action with a state created
awesome_action = Gio.SimpleAction.new_stateful(
"awesome", None, GLib.Variant.new_boolean(False))
# action connected to the callback function
awesome_action.connect("activate", self.awesome_callback)
# action added to the application
self.add_action(awesome_action)
# a builder to add the UI designed with Glade to the grid:
builder = Gtk.Builder()
# get the file (if it is there)
try:
builder.add_from_file("menubar.ui")
except:
print("file not found")
sys.exit()
# we use the method Gtk.Application.set_menubar(menubar) to add the menubar
# and the menu to the application (Note: NOT the window!)
self.set_menubar(builder.get_object("menubar"))
self.set_app_menu(builder.get_object("appmenu"))
# callback function for new
def new_callback(self, action, parameter):
print("You clicked \"New\"")
# callback function for quit
def quit_callback(self, action, parameter):
print("You clicked \"Quit\"")
sys.exit()
# callback function for state
def state_callback(self, action, parameter):
print("State is set to", parameter.get_string())
action.set_state(parameter)
# callback function for awesome
def awesome_callback(self, action, parameter):
action.set_state(GLib.Variant.new_boolean(not action.get_state()))
if action.get_state().get_boolean() is True:
print("You checked \"Awesome\"")
else:
print("You unchecked \"Awesome\"")
app = MyApplication()
exit_status = app.run(sys.argv)
sys.exit(exit_status)
</code>
</section>
<section id="mnemonics-and-accelerators"><title>Mnemonics and Accelerators</title>
<p>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.</p>
<p>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.</p>
<p>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.</p>
<p><code mime="application/xml"><![CDATA[<attribute name="accel"><Primary>q</attribute>]]></code> will create the <keyseq><key>Ctrl</key><key>Q</key></keyseq> sequence when added to the <code>Quit</code> label item. Here, "Primary" refers to the <key>Ctrl</key> key on a PC or the <key>⌘</key> key on a Mac.</p>
<code mime="application/xml"><![CDATA[
<item>
<attribute name="label">_Quit</attribute>
<attribute name="action">app.quit</attribute>
<attribute name="accel"><Primary>q</attribute>
</item>]]></code>
</section>
<section id="translatable"><title>Translatable strings</title>
<p>
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 <code>translatable="yes"</code>:
</p>
<code mime="application/xml"><![CDATA[<attribute name="label" translatable="yes">Quit</attribute>]]></code>
</section>
<section id="references">
<title>API References</title>
<p>Neste exemplo empregaremos o seguinte:</p>
<list>
<item><p><link href="http://developer.gnome.org/gio/unstable/GSimpleAction.html">GSimpleAction</link></p></item>
<item><p><link href="http://developer.gnome.org/gtk3/unstable/GtkBuilder.html">GtkBuilder</link></p></item>
</list>
</section>
</page>
|