Python Tkinter: Passing An Argument To A Function
Can please anyone help me with my problem. I am new to python and I can not get how to pass an argument to a function in the following case: In a separate file (sentiment_analysis)
Solution 1:
You need to capture the current value of p in the lambda:
submenu.add_command(label=p, command = lambda p=p: onButtonPosObject(p), underline=0)
Solution 2:
I'm guessing what's happening is that your positiveSentiments['Motorola'] list is empty. I'd add a print statement in that 'for p in sentiment_analysis.objects' loop to see whats going on. When you use a lambda command in a loop like that, you end up setting the command for each menu item to pass the same value for p (it will be the final value of p) when they call onButtonPosObject.
You need to save the variable to a name local to each lambda, and pass that as the argument (it looks messy, I know): lambda x=p: onButtonPosObject(x)
. Try it and see what you get.
Post a Comment for "Python Tkinter: Passing An Argument To A Function"