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
The PaletteMenuBox is a box that is useful for making palettes. |
|
Horizontal separator to put in a palette. |
|
A palette menu item is a line of text, and optionally an icon, that the |
|
A group of related menu items that can be managed together. |
|
Builder class for creating complex palette menus. |
Functions
|
Create a basic menu item with common parameters. |
Create a menu separator. |
|
|
Create a menu item that expands to show submenu items. |
Module Contents
- class sugar4.graphics.palettemenu.PaletteMenuBox[source]
Bases:
gi.repository.Gtk.BoxThe PaletteMenuBox is a box that is useful for making palettes.
It supports adding
sugar4.graphics.palettemenu.PaletteMenuItem,sugar4.graphics.palettemenu.PaletteMenuItemSeparatorand 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.PaletteMenuItemor asugar4.graphics.palettemenu.PaletteMenuItemSeparator) no padding will be added, as that is handled by the item. If a widget is appended (Gtk.Widgetsubclass) padding will be added.- Parameters:
item_or_widget (
Gtk.Widgetor menu item or separator) – item or widget to add to the palettehorizontal_padding (int) – by default,
sugar4.graphics.style.DEFAULT_SPACINGis appliedvertical_padding (int) – by default,
sugar4.graphics.style.DEFAULT_SPACINGis applied
- Returns:
None
- class sugar4.graphics.palettemenu.PaletteMenuItemSeparator[source]
Bases:
gi.repository.Gtk.SeparatorHorizontal separator to put in a palette.
- 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.ButtonA 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 iconfile_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_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:
- set_image(icon)[source]
Set the icon of the menu item.
- Parameters:
icon (Icon) – Icon widget to display
- sugar4.graphics.palettemenu.create_menu_item(text, icon_name=None, callback=None, accelerator=None)[source]
Create a basic menu item with common parameters.
- Parameters:
- Returns:
The created menu item
- Return type:
- sugar4.graphics.palettemenu.create_separator()[source]
Create a menu separator.
- Returns:
The created separator
- Return type:
- sugar4.graphics.palettemenu.create_submenu_item(text, submenu_items, icon_name=None)[source]
Create a menu item that expands to show submenu items.
- Parameters:
- Returns:
The created submenu item
- Return type:
- class sugar4.graphics.palettemenu.PaletteMenuGroup(name=None)[source]
A group of related menu items that can be managed together.