Skip to content

callback command

hannes edited this page Feb 14, 2023 · 5 revisions

UniMenu uses string commands from a dict / config. But several apps support passing callbacks directly to the menu. This can be usefull when dynamically creating a menu.

Setup env

convenience code: if you don't have unimenu setup in your environment, you can append your unimenu repo path to the system Path, before running below sample code

import sys
sys.path.append(r"C:\Users\hanne\OneDrive\Documents\repos\openmenu")

Creating a menu from a string command

import unimenu

config = {
    "label": "UniMenu",
    "items": [
        {
            "label": "Item 1",
            "command": "print('Item 1')"
        }
    ]
}

app_menu_node = unimenu.setup(config)

Create menu with callback

import unimenu

def hello_world():
    print("hello world")

action_node = unimenu.Node(label="hello_world", command=hello_world)
menu_node = unimenu.Node(label="my menu")
menu_node.children = [action_node]
node = menu_node.setup()
  • Some apps (e.g. Blender) pass an argument to the command it tries to run. Adding *args to the function fixes any errors.
  • not all apps support callback, e.g. Max currently doesn't
Clone this wiki locally