Suppose we have a list of the form:

[ [id, name, pixmap] [id, name, pixmap] ... ] 

The task is to create a toolbar with buttons from this list. Now I just go around the list and add a button, like this:

 for item in items: action = QAction(QIcon(item[2]), item[1], actionG, triggered=lambda: print(item[0])) toolbar.addAction(action) 

I assumed that when the button was pressed, the id of each button would be displayed in the console, and only the last one would be displayed.

The question is: how to properly implement the ability to call a function, let's say print as in the example, so that it knows which button is pressed. Provided that the toolbar is created dynamically based on the list.

  • Are the id's really different in the list? Try to display all the items before iterating over it. - Xander
  • yes, id are exactly different, they are taken from file - Vladimir

1 answer 1

I think I figured it out. Try rewriting lambda like this:

 lambda item=item: print(item[0]) 
  • you were almost right. just need: lambda x, item = item [0]: print (item) in x will be placed True because The button click worked, and in item is the desired parameter. - Vladimir