Blame platform-demos/de/menubutton.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="menubutton.py" xml:lang="de">
Packit 1470ea
  <info>
Packit 1470ea
  <title type="text">MenuButton</title>
Packit 1470ea
    <link type="guide" xref="beginner.py#buttons"/>
Packit 1470ea
    <link type="next" xref="toolbar.py"/>
Packit 1470ea
    <revision version="0.1" date="2012-08-19" 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 widget that shows a menu when clicked on</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>MenuButton</title>
Packit 1470ea
  <media type="image" mime="image/png" src="media/menubutton.png"/>
Packit 1470ea
  

The GtkMenuButton widget is used to display a menu when clicked on. This menu can be provided either as a GtkMenu, or an abstract GMenuModel. The GtkMenuButton widget can hold any valid child widget. That is, it can hold almost any other standard GtkWidget. The most commonly used child is the provided GtkArrow.

Packit 1470ea
Packit 1470ea
  <note>

You need to be running GNOME 3.6 for the MenuButton to work.

</note>
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
    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="Menubutton Example", application=app)
Packit 1470ea
        self.set_default_size(600, 400)
Packit 1470ea
Packit 1470ea
        grid = Gtk.Grid()
Packit 1470ea
Packit 1470ea
        # a menubutton
Packit 1470ea
        menubutton = Gtk.MenuButton()
Packit 1470ea
        menubutton.set_size_request(80, 35)
Packit 1470ea
Packit 1470ea
        grid.attach(menubutton, 0, 0, 1, 1)
Packit 1470ea
Packit 1470ea
        # a menu with two actions
Packit 1470ea
        menumodel = Gio.Menu()
Packit 1470ea
        menumodel.append("New", "app.new")
Packit 1470ea
        menumodel.append("About", "win.about")
Packit 1470ea
Packit 1470ea
        # a submenu with one action for the menu
Packit 1470ea
        submenu = Gio.Menu()
Packit 1470ea
        submenu.append("Quit", "app.quit")
Packit 1470ea
        menumodel.append_submenu("Other", submenu)
Packit 1470ea
Packit 1470ea
        # the menu is set as the menu of the menubutton
Packit 1470ea
        menubutton.set_menu_model(menumodel)
Packit 1470ea
Packit 1470ea
        # the action related to the window (about)
Packit 1470ea
        about_action = Gio.SimpleAction.new("about", None)
Packit 1470ea
        about_action.connect("activate", self.about_callback)
Packit 1470ea
        self.add_action(about_action)
Packit 1470ea
Packit 1470ea
        self.add(grid)
Packit 1470ea
Packit 1470ea
    # callback for "about"
Packit 1470ea
    def about_callback(self, action, parameter):
Packit 1470ea
        print("You clicked \"About\"")
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
        Gtk.Application.do_startup(self)
Packit 1470ea
Packit 1470ea
        #  the actions related to the application
Packit 1470ea
        new_action = Gio.SimpleAction.new("new", None)
Packit 1470ea
        new_action.connect("activate", self.new_callback)
Packit 1470ea
        self.add_action(new_action)
Packit 1470ea
Packit 1470ea
        quit_action = Gio.SimpleAction.new("quit", None)
Packit 1470ea
        quit_action.connect("activate", self.quit_callback)
Packit 1470ea
        self.add_action(quit_action)
Packit 1470ea
Packit 1470ea
    # callback functions for the actions related to the application
Packit 1470ea
    def new_callback(self, action, parameter):
Packit 1470ea
        print("You clicked \"New\"")
Packit 1470ea
Packit 1470ea
    def quit_callback(self, action, parameter):
Packit 1470ea
        print("You clicked \"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 MenuButton widget</title>
Packit 1470ea
      

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

Packit 1470ea
Packit 1470ea
    

The positioning of the menu is determined by the "direction" property of the menu button and the "halign" or "valign" properties of the menu. For example, when the direction is Gtk.ArrowType.DOWN (other option: UP) and the horizontal alignment is Gtk.Align.START (other options: CENTER and END), the menu will be positioned below the button, with the starting edge (depending on the text direction) of the menu aligned with the starting edge of the button. If there is not enough space below the button, the menu is popped up above the button instead. If the alignment would move part of the menu offscreen, it is 'pushed in'.

Packit 1470ea
    
Packit 1470ea
    

In the case of vertical alignment, the possible ArrowType directions are LEFT and RIGHT and the vertical alignment is again START, CENTER or END.

Packit 1470ea
    
Packit 1470ea
    

set_align_widget(alignment) and set_direction(direction) can be used to set these properties.

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/gtk3/unstable/GtkMenuButton.html">MenuButton</link>

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