create_menu
Create a menu with icon and theme support.
This is a convenience function that creates or retrieves a MenuManager for the parent window and uses it to build a menu from a declarative structure. The resulting menu automatically updates icon colors when the theme changes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parent
|
Any
|
The parent widget (Window, Toplevel, or Menu). If a Window or Toplevel is provided, the menu becomes the window's menubar. If a Menu is provided, items are added to that menu. |
required |
items
|
list[dict]
|
List of dictionaries defining the menu structure. Each dictionary represents a menu item with its configuration. |
required |
Returns:
| Type | Description |
|---|---|
Menu
|
The created Menu object. For window menubars, this is the menubar |
Menu
|
itself. For menus attached to other widgets, this is the menu. |
Examples:
Basic menubar with icons:
import bootstack as bs
app = bs.App()
menu_items = [
{
"label": "File",
"items": [
{"label": "New", "icon": "file-plus", "command": new_file},
{"label": "Open", "icon": "folder2-open", "command": open_file},
{"type": "separator"},
{"label": "Exit", "icon": "x-circle", "command": app.quit}
]
},
{
"label": "Edit",
"items": [
{"label": "Undo", "icon": "arrow-counterclockwise"},
{"label": "Redo", "icon": "arrow-clockwise"}
]
}
]
bs.create_menu(app, menu_items)
app.mainloop()
Nested submenus with custom icon sizes:
menu_items = [
{
"label": "File",
"items": [
{
"label": "Recent",
"icon": {"name": "clock-history", "size": 18},
"items": [
{"label": "Document 1.txt"},
{"label": "Document 2.txt"}
]
}
]
}
]
Menu with checkbuttons and radiobuttons:
view_var = bs.BooleanVar(value=True)
theme_var = bs.StringVar(value="light")
menu_items = [
{
"label": "View",
"items": [
{
"label": "Show Toolbar",
"type": "checkbutton",
"variable": view_var
}
]
},
{
"label": "Theme",
"items": [
{
"label": "Light",
"type": "radiobutton",
"variable": theme_var,
"value": "light"
},
{
"label": "Dark",
"type": "radiobutton",
"variable": theme_var,
"value": "dark"
}
]
}
]