Expander

The MenuButton widget displays a menu when the button is clicked.

Constructor

The MenuButton can be constructed using the following:

menubutton = Gtk.MenuButton()

Methods

A MenuButton can have a Menu added to it:

menubutton.set_popup(menu)

By default, the menu appears beneath the MenuButton, however this can be configured:

menubutton.set_direction(direction)

The direction parameter can be set to one of the following:

  • Gtk.ArrowType.NONE
  • Gtk.ArrowType.DOWN
  • Gtk.ArrowType.UP
  • Gtk.ArrowType.LEFT
  • Gtk.ArrowType.RIGHT

Example

Below is an example of a MenuButton:

#!/usr/bin/env python3

from gi.repository import Gtk

class MenuButton(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self)
        self.connect("destroy", Gtk.main_quit)

        menubutton = Gtk.MenuButton("MenuButton")
        self.add(menubutton)

        menu = Gtk.Menu()
        menubutton.set_popup(menu)

        for count in range(1, 6):
            menuitem = Gtk.MenuItem("Item %i" % (count))
            menuitem.connect("activate", self.on_menuitem_activated)
            menu.append(menuitem)

        menu.show_all()

    def on_menuitem_activated(self, menuitem):
        print("%s Activated" % (menuitem.get_label()))

window = MenuButton()
window.show_all()

Gtk.main()

Download: MenuButton