MenuHandler for Python

Hello Matthias

Looks like the MenuHandler code is being used quite often.
Can we get the equivalent of the Ruby code below for Python?

Thanks!

class MenuHandler < RBA::Action
def initialize( t, k, i, &action )
self.title = t
self.shortcut = k
self.icon = i
@action = action
end
def triggered
@action.call( self )
end
private
@action
end

menu.insert_item("@toolbar.end", "menu_item3", $menu_handler_transparent)
menu.insert_item("tools_menu.end", "menu_item3", $menu_handler_transparent)

Comments

  • Hi Mikamar,

    Since some time now, the Action class does not need to be subclassed to implement a menu function. It's sufficient to use the "on_triggered" callback:

    def handler_func():
      pya.MessageBox.info("Info", "Menu action called!", pya.MessageBox.Ok)
    
    menu_handler = pya.Action()
    menu_handler.title = "My toolbar function"
    menu_handler.on_triggered = handler_func
    
    menu = pya.Application.instance().main_window().menu()
    menu.insert_item("@toolbar.end", "menu_item3", menu_handler)
    menu.insert_item("tools_menu.end", "menu_item3", menu_handler)
    

    Matthias

  • Thank you Matthias!

Sign In or Register to comment.