I am new to python and encounter a problem with a small piece of code aiming at implementing a menu with Qt:
def setChoice(self,choice=None):
do something here...
listOfChoices=[choice1,choice2,choice3]
menu=QMenu(self)
for choice in listOfChoices:
action=menu.addAction(choice)
action.triggered.connect(lambda : self.setChoice(choice=choice))
The problem is that the setChoice()
function is always called with choice=choice3
, the last choice of the for loop. How to solve this problem properly ?
And a probably related question about immediate partial evaluation:
How to save in memory f= lambda x : x+1
from a piece of code like a=1; f=lambda x: x+a
?
Thanks for your explanations.
Denis