Skip to content

callback command

hannes edited this page Feb 15, 2023 · 5 revisions

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

String command:

command = "print('Item 1')"

Callback:

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

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