# MenuButton

Tiffany Antopolski
tiffany.antopolski@gmail.com 2012
Marta Maria Casetti
mmcasetti@gmail.com 2012
A widget that shows a menu when clicked on
Rafael Ferreira
rafael.f.f1@gmail.com 2013

# MenuButton

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.

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

# Code used to generate this example

from gi.repository import Gtk from gi.repository import Gio import sys

class MyWindow(Gtk.ApplicationWindow):

def __init__(self, app): Gtk.Window.__init__(self, title="Menubutton Example", application=app) self.set_default_size(600, 400)

grid = Gtk.Grid()

# a menubutton menubutton = Gtk.MenuButton() menubutton.set_size_request(80, 35)

grid.attach(menubutton, 0, 0, 1, 1)

# a menu with two actions menumodel = Gio.Menu() menumodel.append("New", "app.new") menumodel.append("About", "win.about")

# a submenu with one action for the menu submenu = Gio.Menu() submenu.append("Quit", "app.quit") menumodel.append_submenu("Other", submenu)

# the menu is set as the menu of the menubutton menubutton.set_menu_model(menumodel)

# the action related to the window (about) about_action = Gio.SimpleAction.new("about", None) about_action.connect("activate", self.about_callback) self.add_action(about_action)

self.add(grid)

# callback for "about" def about_callback(self, action, parameter): print("You clicked \"About\"")

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)

# the actions related to the application new_action = Gio.SimpleAction.new("new", None) new_action.connect("activate", self.new_callback) self.add_action(new_action)

quit_action = Gio.SimpleAction.new("quit", None) quit_action.connect("activate", self.quit_callback) self.add_action(quit_action)

# callback functions for the actions related to the application def new_callback(self, action, parameter): print("You clicked \"New\"")

def quit_callback(self, action, parameter): print("You clicked \"Quit\"") self.quit()

app = MyApplication() exit_status = app.run(sys.argv) sys.exit(exit_status)

# Useful methods for a MenuButton widget

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 for a more detailed explanation.

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'.

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

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

# API References

In this sample we used the following:

MenuButton