Create a menu from callbacks

It is possible to create menu items for functions in your code. You only need to pass a list of tuples (menu item string, function identifier) to the MenuBuilder class constructor. Check the following example:

from mojo.UI import MenuBuilder

def foo(sender):
    print(sender.title())


if __name__ == '__main__':
    menu = MenuBuilder(menu="main",
                       title="bar",
                       insert=5,
                       items=[("foo1", foo),
                              ("foo2", foo)]
                       )

Create a menu from a folder with scripts

The MenuBuilder class has the useful method .addMenuFromPath() for adding a folder of python scripts to the application menu

from mojo.UI import MenuBuilder

folderPath = 'folderWithScripts'

if __name__ == '__main__':
    menu = MenuBuilder(menu="main", title="test")
    menu.addMenuFromPath(folderPath)

Both snippets can be set as startup scripts from the application preferences menu

mojo.ui MenuBuilder reference

Last edited on 01/09/2021