Each display element can have its own custom menu by setting the menu property.
A menu is a list of MenuItem objects. To create a menu item, use the MenuItem constructor.
mymenu = [MenuItem("/item1", "New", icon = "gfx/new.png", callback = do_new), MenuItem("/item2", "Open", icon = "gfx/open.png", callback = do_open), MenuItem("/item3", "Save", icon = "gfx/save.png", callback = do_save)]
The MenuItem constructor takes a bunch of arguments, of which many are optional. Menu items can have:
These are arguments to the constructor, but also properties of the MenuItem objects. So it's possible to change values any time by assigning new values to the properties.
Each item has a unique path within the menu. This path is used for setting submenus. Each path begins with a slash ("/") character and is constructed similar to a filesystem path, e.g. "/mymenu".
In order to add a submenu, you simply add a new item with an extended path, e.g. "/mymenu/mysubmenu". gDesklets will setup the menu structure automatically, so you don't have to bother with attaching submenus to menu items manually.
mymenu = [MenuItem("/item1", "New", icon = "gfx/new.png", callback = do_new), MenuItem("/item2", "Open", icon = "gfx/open.png"), MenuItem("/item2/foo", "from file", callback = do_open_file), MenuItem("/item2/bar", "from URL", callback = do_open_url), MenuItem("/item3", "Save", icon = "gfx/save.png", callback = do_save)]
Menu items with submenus are not clickable and thus don't need any callback handler, of course.
![]() |
|
Navigating submenus with the mouse is considered difficult by many users. If possible, you should avoid making deeply nested submenus! |
A menu item with only a path will be displayed as a separator line.
mymenu = [MenuItem("/item1", "New", icon = "gfx/new.png", callback = do_new), MenuItem("/sep"), MenuItem("/item2", "Open", icon = "gfx/open.png", callback = do_open), MenuItem("/item3", "Save", icon = "gfx/save.png", callback = do_save)]
Whenever you assign a list of MenuItem objects to the menu property of a display element, that menu will be displayed.
... <label value="Blah" on-menu="self.menu = mymenu"/> ... <script> def do_new(): ... def do_open(): ... def do_save(): ... mymenu = [MenuItem("/item1", "New", icon = "gfx/new.png", callback = do_new), MenuItem("/sep"), MenuItem("/item2", "Open", icon = "gfx/open.png", callback = do_open), MenuItem("/item3", "Save", icon = "gfx/save.png", callback = do_save)] </script> ...