可以请任何人帮助我解决我的问题。我是 python 的新手,在以下情况下我无法获得如何将参数传递给函数:在单独的文件 (sentiment_analysis) 中,我有一个字典和一个对象数组:
positiveSentiments = dict() // here are some words related to each of the object
objects = ['Google', 'Apple', 'Motorola']
Run Code Online (Sandbox Code Playgroud)
我需要显示每个对象的积极情绪:
def onButtonPosObject(p):
for key in sentiment_analysis.positiveSentiments.keys():
if key == p:
text.insert(END, sentiment_analysis.positiveSentiments[key])
submenu = Menu(text, tearoff=0)
for p in sentiment_analysis.objects:
submenu.add_command(label=p, command = lambda : onButtonPosObject(p), underline=0)
textmenu.add_cascade(label='Display positive sentiments', menu=submenu, underline=0)
Run Code Online (Sandbox Code Playgroud)
我想我必须传递标签 (p) 的值作为 onButtonPosObject() 函数的参数,我需要从 positiveSentiments 字典中为每个对象获取单词列表,但我得到像 [] 这样的空值。如有任何建议,我将不胜感激!
您需要在 lambda 中捕获 p 的当前值:
submenu.add_command(label=p, command = lambda p=p: onButtonPosObject(p), underline=0)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1892 次 |
| 最近记录: |