Trigger build-in action with a button

edited May 16 in Python scripting

Hi KLayout community, Hi @Matthias,

I tried to convert the Ruby code from https://www.klayout.de/forum/discussion/616/add-horizontal-vertical-alignement-on-toolbar to a python code.

Basically the code should add a button to the toolbar, which triggers the the align action from the selection menu.
The code below indeed adds a button to the toolbar and when the button is pressed, the message "The action was triggered" is displayed.
However, the align action is not triggered. I suspect the line menu.action("edit_menu.selection_menu.align").trigger is not correct. Do you know a way to trigger the align action by a new button with python?

Of course I could always use the ruby code, but I try to focus on one coding language and I'd also like to understand the integration with python.

from pya import Action, MessageBox, Application

menu = Application.instance().main_window().menu()

def on_triggered():
  menu.action("edit_menu.selection_menu.align").trigger
  MessageBox.info("A message", "The action was triggered", MessageBox.Ok)

act = Action()
act.on_triggered = on_triggered
act.title = "My Action"

menu.insert_item("@toolbar.end", "my_action", act)

Comments

  • Hello, Sepbe

    Probably you try to do this?

    import pya
    
    def on_triggered():
      pya.MessageBox.info("A message", "The action was triggered", pya.MessageBox.Ok)
    
    act = pya.Action()
    act.title = "My Action"
    act.on_triggered = on_triggered
    
    menu = pya.Application.instance().main_window().menu()
    menu.insert_item("@toolbar.end", "my_action", act)
    

    Vincent

  • edited May 16

    Hi @Vincent,

    thanks, but the important part is the line "pya.Application.instance().main_window().menu().action("edit_menu.selection_menu.align").trigger", which does not seem open the align options if the button is clicked.

    import pya
    
    def on_triggered():
      pya.Application.instance().main_window().menu().action("edit_menu.selection_menu.align").trigger
    
    act = pya.Action()
    act.title = "My Action"
    act.on_triggered = on_triggered
    
    menu = pya.Application.instance().main_window().menu()
    menu.insert_item("@toolbar.end", "my_action", act)
    

    Sepbe

  • Hello, Sepbe

    I love Ruby more, I have changed my mind :/

    import pya
    
    def on_triggered():
      pya.MessageBox.info("A message", "The action was triggered", pya.MessageBox.Ok)
      menu.action("edit_menu.selection_menu.align").trigger()
    
    act = pya.Action()
    act.title = "My Action"
    act.on_triggered = on_triggered
    
    menu = pya.Application.instance().main_window().menu()
    menu.insert_item("@toolbar.end", "my_action", act)
    

    Vincent

  • @Vincent Lin

    I had the same issue myself too many times ... you get used to that :)

    Regarding your solution: a cheap way of getting the same effect is:

    menu = pya.MainWindow.instance().menu()
    menu.insert_item("@toolbar.end", "my_action", menu.action("edit_menu.selection_menu.align"))
    

    Regards,

    Matthias

Sign In or Register to comment.