sugar3.graphics.palettemenu module

The palettemenu module is the main port of call for making palettes. It covers creating menu items, seperators and placing them in a box.

Example

Create a palette menu with 2 items with a seperator in the middle.

from gi.repository import Gtk
from gettext import gettext as _

from sugar3.graphics.palette import Palette
from sugar3.graphics.palettemenu import PaletteMenuBox
from sugar3.graphics.palettemenu import PaletteMenuItem
from sugar3.graphics.palettemenu import PaletteMenuItemSeparator


class ItemPalette(Palette):
    def __init__(self):
        Palette.__init__(
            self, primary_text='List Item')
        box = PaletteMenuBox()
        self.set_content(box)
        box.show()

        menu_item = PaletteMenuItem(
            _('Edit'), icon_name='toolbar-edit')
        menu_item.connect('activate', self.__edit_cb)
        box.append_item(menu_item)
        menu_item.show()

        sep = PaletteMenuItemSeparator()
        box.append_item(sep)
        sep.show()

        menu_item = PaletteMenuItem(
            _('Delete'), icon_name='edit-delete')
        box.append_item(menu_item)
        menu_item.show()

    def __edit_cb(self, menu_item):
        print('Edit...')

# Usually the Palette instance is returned in a create_palette function
p = ItemPalette()
p.popup()
Gtk.main()

Add a palettebox to a toolbutton:

image = ToolButton('insert-picture')
image.set_tooltip(_('Insert Image'))
self._image_id = image.connect('clicked', self.__image_cb)
toolbar_box.toolbar.insert(image, -1)

palette = image.get_palette()
box = PaletteMenuBox()
palette.set_content(box)
box.show()

menu_item = PaletteMenuItem(_('Floating'))
menu_item.connect('activate', self.__image_cb, True)
box.append_item(menu_item)
menu_item.show()
class sugar3.graphics.palettemenu.PaletteMenuBox

Bases: gi.repository.Gtk.VBox

The PaletteMenuBox is a box that is useful for making palettes. It supports adding sugar3.graphics.palettemenu.PaletteMenuItem, sugar3.graphics.palettemenu.PaletteMenuItemSeparator and it automatically adds padding to other widgets.

append_item(item_or_widget, horizontal_padding=None, vertical_padding=None)

Add a menu item, seperator or other widget to the end of the palette (simmilar to Gtk.Box.pack_start).

If an item is appended (a sugar3.graphics.palettemenu.PaletteMenuItem or a sugar3.graphics.palettemenu.PaletteMenuItemSeparator) no padding will be added, as that is handled by the item. If a widget is appended (Gtk.Widget subclass) padding will be added.

Parameters
Returns

None

class sugar3.graphics.palettemenu.PaletteMenuItem(text_label=None, icon_name=None, text_maxlen=60, xo_color=None, file_name=None, accelerator=None)

Bases: gi.repository.Gtk.EventBox

A palette menu item is a line of text, and optionally an icon, that the user can activate.

The activate signal is usually emitted when the item is clicked. It has no arguments. When a menu item is activated, the palette is also closed.

Parameters
  • text_label (str) – a text to display in the menu

  • icon_name (str) – the name of a sugar icon to be displayed. Takse precedence over file_name

  • text_maxlen (int) – the desired maximum width of the label, in characters. By default set to 60 chars

  • xo_color (sugar.graphics.XoColor) – the color to be applied to the icon

  • file_name (str) – the path to a svg file used as icon

  • accelerator (str) – a text used to display the keyboard shortcut associated to the menu

set_accelerator(text)

Sets the text used to display the keyboard shortcut associated with the menu.

Parameters

text (str) – accelerator text

set_image(icon)

Sets the icon widget. Usually this will be a sugar3.graphics.icon.Icon.

Parameters

icon (Gtk.Widget) – icon widget

set_label(text_label)

Sets the text to display in the menu.

Parameters

text_label (str) – text label

set_sensitive(sensitive)

Sets whether the widget should be activateable by the user and changes the widget’s appearence to the appropriate state.

Parameters

sensitive (bool) – if True, the widget will be activateable by the user. Otherwise, it will not be activateable

class sugar3.graphics.palettemenu.PaletteMenuItemSeparator

Bases: gi.repository.Gtk.EventBox

Horizontal seperator to put in a palette