sugar4.graphics.palettemenu

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

This implementation modernizes the palette menu system while maintaining compatibility with Sugar’s palette interface patterns.

Example

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

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

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


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

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

        sep = PaletteMenuItemSeparator()
        box.append_item(sep)

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

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

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

Add a palettebox to a toolbutton:

image = ToolButton('insert-picture')
image.set_tooltip(_('Insert Image'))
toolbar_box.toolbar.insert(image, -1)

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

menu_item = PaletteMenuItem(_('Floating'))
menu_item.connect('activate', self.__image_cb, True)
box.append_item(menu_item)

Classes

PaletteMenuBox

The PaletteMenuBox is a box that is useful for making palettes.

PaletteMenuItemSeparator

Horizontal separator to put in a palette.

PaletteMenuItem

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

PaletteMenuGroup

A group of related menu items that can be managed together.

PaletteMenuBuilder

Builder class for creating complex palette menus.

Functions

create_menu_item(text[, icon_name, callback, accelerator])

Create a basic menu item with common parameters.

create_separator()

Create a menu separator.

create_submenu_item(text, submenu_items[, icon_name])

Create a menu item that expands to show submenu items.

Module Contents

class sugar4.graphics.palettemenu.PaletteMenuBox[source]

Bases: gi.repository.Gtk.Box

The PaletteMenuBox is a box that is useful for making palettes.

It supports adding sugar4.graphics.palettemenu.PaletteMenuItem, sugar4.graphics.palettemenu.PaletteMenuItemSeparator and it automatically adds padding to other widgets.

append_item(item_or_widget, horizontal_padding=None, vertical_padding=None)[source]

Add a menu item, separator or other widget to the end of the palette (similar to Gtk.Box.append).

If an item is appended (a sugar4.graphics.palettemenu.PaletteMenuItem or a sugar4.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 sugar4.graphics.palettemenu.PaletteMenuItemSeparator[source]

Bases: gi.repository.Gtk.Separator

Horizontal separator to put in a palette.

__gtype_name__ = 'SugarPaletteMenuItemSeparator'[source]
class sugar4.graphics.palettemenu.PaletteMenuItem(text_label=None, icon_name=None, text_maxlen=60, xo_color=None, file_name=None, accelerator=None)[source]

Bases: gi.repository.Gtk.Button

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.

This implementation replaces EventBox with Button for better accessibility and modern interaction patterns.

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

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

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

  • xo_color (sugar4.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

__gtype_name__ = 'SugarPaletteMenuItem'[source]
__gsignals__[source]
icon = None[source]
set_label(text_label)[source]

Set the text label of the menu item.

Parameters:

text_label (str) – New text to display

get_label()[source]

Get the current text of the menu item.

Returns:

Current text or empty string

Return type:

str

set_image(icon)[source]

Set the icon of the menu item.

Parameters:

icon (Icon) – Icon widget to display

set_accelerator(text)[source]

Set the accelerator text for the menu item.

Parameters:

text (str) – Accelerator text to display (e.g., “Ctrl+S”)

set_sensitive(sensitive)[source]

Set the sensitivity of the menu item.

Parameters:

sensitive (bool) – Whether the item should be sensitive

sugar4.graphics.palettemenu.create_menu_item(text, icon_name=None, callback=None, accelerator=None)[source]

Create a basic menu item with common parameters.

Parameters:
  • text (str) – Menu item text

  • icon_name (str) – Optional icon name

  • callback (function) – Optional callback function

  • accelerator (str) – Optional accelerator text

Returns:

The created menu item

Return type:

PaletteMenuItem

sugar4.graphics.palettemenu.create_separator()[source]

Create a menu separator.

Returns:

The created separator

Return type:

PaletteMenuItemSeparator

sugar4.graphics.palettemenu.create_submenu_item(text, submenu_items, icon_name=None)[source]

Create a menu item that expands to show submenu items.

Parameters:
  • text (str) – Menu item text

  • submenu_items (list) – List of submenu items

  • icon_name (str) – Optional icon name

Returns:

The created submenu item

Return type:

PaletteMenuItem

class sugar4.graphics.palettemenu.PaletteMenuGroup(name=None)[source]

A group of related menu items that can be managed together.

name = None[source]
items = [][source]
add_item(item)[source]

Add an item to this group.

set_sensitive(sensitive)[source]

Set sensitivity of all items in the group.

set_visible(visible)[source]

Set visibility of all items in the group.

class sugar4.graphics.palettemenu.PaletteMenuBuilder[source]

Builder class for creating complex palette menus.

menu_box[source]
groups[source]
add_item(text, icon_name=None, callback=None, accelerator=None, group=None)[source]

Add a menu item to the builder.

add_separator()[source]

Add a separator to the builder.

get_menu_box()[source]

Get the constructed menu box.

get_group(name)[source]

Get a menu group by name.