MenuButton Tiffany Antopolski tiffany.antopolski@gmail.com 2012 Marta Maria Casetti mmcasetti@gmail.com 2012 Un élément graphique qui affiche un menu quand il est cliqué Luc Rebert, traduc@rebert.name 2011 Alain Lojewski, allomervan@gmail.com 2011-2012 Luc Pionchon pionchon.luc@gmail.com 2011 Bruno Brouard annoa.b@gmail.com 2011-12 Luis Menina liberforce@freeside.fr 2014 MenuButton

L'élément graphique GtkMenuButton sert à afficher un menu quand il est cliqué. Le menu peut provenir soit d'un GtkMenu, soit d'un GMenuModel abstrait. L'élément graphique GtkMenuButton peut contenir n'importe quel élément graphique enfant valide. Ceci dit, il peut contenir pratiquement n'importe quel autre GtkWidget standard. L'enfant le plus souvent utilisé est la flèche GtkArrow fournie.

Pour que le MenuButton fonctionne, il vous faut avoir installé GNOME 3.6

Code utilisé pour générer cet exemple 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.

Références API

Dans cet exemple, les éléments suivants sont utilisés :

MenuButton