Blame platform-demos/de/gmenu.py.page

Packit 1470ea
Packit 1470ea
<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="gmenu.py" xml:lang="de">
Packit 1470ea
  <info>
Packit 1470ea
    <title type="text">GMenu (Python)</title>
Packit 1470ea
    <link type="guide" xref="beginner.py#menu-combo-toolbar"/>
Packit 1470ea
    <link type="seealso" xref="signals-callbacks.py"/>
Packit 1470ea
    <link type="next" xref="menubutton.py"/>
Packit 1470ea
    <revision version="0.1" date="2012-04-28" status="draft"/>
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
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 simple implementation of GMenu</desc>
Packit 1470ea
  
Packit 1470ea
    <mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
Packit 1470ea
      <mal:name>Mario Blättermann</mal:name>
Packit 1470ea
      <mal:email>mario.blaettermann@gmail.com</mal:email>
Packit 1470ea
      <mal:years>2011, 2013</mal:years>
Packit 1470ea
    </mal:credit>
Packit 1470ea
  </info>
Packit 1470ea
Packit 1470ea
  <title>GMenu</title>
Packit 1470ea
  <media type="image" mime="image/png" src="media/gmenu.py.png"/>
Packit 1470ea
  

A GtkApplication with a simple GMenu and SimpleActions

Packit 1470ea
Packit 1470ea
  <links type="section"/>
Packit 1470ea
Packit 1470ea
  <section id="code">
Packit 1470ea
    <title>Code used to generate this example</title>
Packit 1470ea
    
Packit 1470ea
    from gi.repository import Gtk
Packit 1470ea
from gi.repository import Gio
Packit 1470ea
import sys
Packit 1470ea
Packit 1470ea
Packit 1470ea
class MyWindow(Gtk.ApplicationWindow):
Packit 1470ea
Packit 1470ea
    def __init__(self, app):
Packit 1470ea
        Gtk.Window.__init__(self, title="GMenu Example", application=app)
Packit 1470ea
Packit 1470ea
Packit 1470ea
class MyApplication(Gtk.Application):
Packit 1470ea
Packit 1470ea
    def __init__(self):
Packit 1470ea
        Gtk.Application.__init__(self)
Packit 1470ea
Packit 1470ea
    def do_activate(self):
Packit 1470ea
        win = MyWindow(self)
Packit 1470ea
        win.show_all()
Packit 1470ea
Packit 1470ea
    def do_startup(self):
Packit 1470ea
        # start the application
Packit 1470ea
        Gtk.Application.do_startup(self)
Packit 1470ea
Packit 1470ea
        # create a menu
Packit 1470ea
        menu = Gio.Menu()
Packit 1470ea
        # append to the menu three options
Packit 1470ea
        menu.append("New", "app.new")
Packit 1470ea
        menu.append("About", "app.about")
Packit 1470ea
        menu.append("Quit", "app.quit")
Packit 1470ea
        # set the menu as menu of the application
Packit 1470ea
        self.set_app_menu(menu)
Packit 1470ea
Packit 1470ea
        # create an action for the option "new" of the menu
Packit 1470ea
        new_action = Gio.SimpleAction.new("new", None)
Packit 1470ea
        # connect it to the callback function new_cb
Packit 1470ea
        new_action.connect("activate", self.new_cb)
Packit 1470ea
        # add the action to the application
Packit 1470ea
        self.add_action(new_action)
Packit 1470ea
Packit 1470ea
        # option "about"
Packit 1470ea
        about_action = Gio.SimpleAction.new("about", None)
Packit 1470ea
        about_action.connect("activate", self.about_cb)
Packit 1470ea
        self.add_action(about_action)
Packit 1470ea
Packit 1470ea
        # option "quit"
Packit 1470ea
        quit_action = Gio.SimpleAction.new("quit", None)
Packit 1470ea
        quit_action.connect("activate", self.quit_cb)
Packit 1470ea
        self.add_action(quit_action)
Packit 1470ea
Packit 1470ea
    # callback function for "new"
Packit 1470ea
    def new_cb(self, action, parameter):
Packit 1470ea
        print("This does nothing. It is only a demonstration.")
Packit 1470ea
Packit 1470ea
    # callback function for "about"
Packit 1470ea
    def about_cb(self, action, parameter):
Packit 1470ea
        print("No AboutDialog for you. This is only a demonstration.")
Packit 1470ea
Packit 1470ea
    # callback function for "quit"
Packit 1470ea
    def quit_cb(self, action, parameter):
Packit 1470ea
        print("You have quit.")
Packit 1470ea
        self.quit()
Packit 1470ea
Packit 1470ea
app = MyApplication()
Packit 1470ea
exit_status = app.run(sys.argv)
Packit 1470ea
sys.exit(exit_status)
Packit 1470ea
Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="methods">
Packit 1470ea
    <title>Useful methods for a GSimpleAction and a GMenu</title>
Packit 1470ea
Packit 1470ea
    

In line 33 the signal "activate" from the action new_action (not the menu!) is connected to the callback function new_cb() using action.connect(signal, callback function). See <link xref="signals-callbacks.py"/> for a more detailed explanation.

Packit 1470ea
Packit 1470ea
    

Useful methods for a GSimpleAction:

Packit 1470ea
    <list>
Packit 1470ea
      <item>

To create a new action that is stateless, that is, an action that do not retain or depend on a state given by the action itself, use

Packit 1470ea
      
Packit 1470ea
action = Gio.SimpleAction.new("name", parameter_type)
Packit 1470ea
      

where "name" is the name of the action and parameter_type is the type of the parameters that the action receives when being activated. This can be None, or GLib.VariantType.new('s') if the parameter is of type str, or instead of 's' a character as described <link href="http://developer.gnome.org/glib/unstable/glib-GVariantType.html">here</link>. To create a new stateful (i.e. not stateless) action, use

Packit 1470ea
      
Packit 1470ea
action = Gio.SimpleAction.new_stateful("name", parameter_type, initial_state)
Packit 1470ea
      

where initial_state is defined as a GVariant - for instance Glib.Variant.new_string('start'); for a list of possibilities see <link href="http://developer.gnome.org/glib/unstable/glib-GVariant.html">here</link>.

</item>
Packit 1470ea
      <item>

set_enabled(True) sets the action as enabled; an action must be enabled in order to be activated or in order to have its state changed from outside callers. This should only be called by the implementor of the action. Users of the action should not attempt to modify its enabled flag.

</item>
Packit 1470ea
      <item>

set_state(state), where state is a GVariant, sets the state of the action, updating the 'state' property to the given value. This should only be called by the implementor of the action; users of the action should instead call change_state(state) (where state is as above) to request the change.

</item>
Packit 1470ea
    </list>
Packit 1470ea
Packit 1470ea
    

Useful methods for a GMenu:

Packit 1470ea
    <list>
Packit 1470ea
      <item>

To insert an item in the menu in position position, use insert(position, label, detailed_action), where label is the label that will appear in the menu and detailed_action is a string composed of the name of the action to which we prepend the prefix app.. A more detailed discussion of this can be found in <link xref="menubar.py#win-app"/>.

Packit 1470ea
      

To append or prepend an item in the menu use respectively append(label, detailed_action) and prepend(label, detailed_action).

</item>
Packit 1470ea
      <item>

Another way of adding items to the menu is to create them as GMenuItems and use insert_item(position, item), append_item(item), or prepend_item(item); so for instance we might have:

Packit 1470ea
      
Packit 1470ea
about = Gio.MenuItem.new("About", "app.about")
Packit 1470ea
menu.append_item(about)
Packit 1470ea
      </item>
Packit 1470ea
      <item>

We can also add a whole subsection in a menu using insert_section(position, label, section), append_section(label, section), or prepend_section(label, section), where label is the title of the subsection.

</item>
Packit 1470ea
      <item>

To add a submenu that will expand and collapse, use insert_submenu(position, label, section), append_submenu(label, section), or prepend_submenu(label, section), where label is the title of the subsection.

</item>
Packit 1470ea
      <item>

To remove an item from the menu, use remove(position).

</item>
Packit 1470ea
      <item>

To set a label for the menu, use set_label(label).

</item>
Packit 1470ea
    </list>
Packit 1470ea
Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="references">
Packit 1470ea
    <title>API-Referenzen</title>
Packit 1470ea
    

In this sample we used the following:

Packit 1470ea
    <list>
Packit 1470ea
      <item>

<link href="http://developer.gnome.org/gio/unstable/GMenu.html">GMenu</link>

</item>
Packit 1470ea
      <item>

<link href="http://developer.gnome.org/gio/stable/GSimpleAction.html">GSimpleAction</link>

</item>
Packit 1470ea
      <item>

<link href="http://developer.gnome.org/glib/unstable/glib-GVariantType.html">GVariantType</link>

</item>
Packit 1470ea
      <item>

<link href="http://developer.gnome.org/glib/unstable/glib-GVariant.html">GVariant</link>

</item>
Packit 1470ea
    </list>
Packit 1470ea
  </section>
Packit 1470ea
</page>