I’m writing a simple clipboard manager in wxpython for fun and profit and I’ve run across a peculiar bug that is causing the same function to be called no matter which menu item is clicked.
The code is here and problem is related to lines 49-56 (the “Clear” item works just fine) – it always calls the last function created.
I tried printing the lambda function being created, the menu item, it’s ID – pretty much everything that I could think of that could have been the source of problem, but I didn’t find any clues.
I’m kinda stuck if I can’t choose menu items so I’d appreciate all and any help.
The value of
iis being looked-up in the enclosing scope ofCreateHistoryMenu. When thefuncis called,iequals the last value in the loop. That is why the menu items all call the last function created.If you add
ito the arguments of thelambdawith a default value,iwill become a local variable in thelambdaitself, with a default value which was bound at the time the lambda was defined. This will be the right value fori: