我已经创建了一个小GUI,我想在其中创建一个循环中的按钮,用来打开一个文本文件。打开textfile的函数位于另一个python模块中,我包括了:
def openTextEditor(textfile):
"""opens the textfile with path 'textfile' """
[...]
在实际循环中,我尝试为按钮分配不同的路径:
for path in foobar:
print path
tk.Button(text="EDIT",
relief="tk.RAISED",
bg='black',
fg='white',
command= lambda: openTextEditor(path) ).grid(row=r, column=c)
从this post我知道必须使用lamda表达式才能将参数作为button命令传递。你知道吗
但是,我的控制台打印到文本文件的不同路径:
path1
path2
path3
而我的3个按钮都用路径3打开文本文件。我假设只有lambda函数的最后一个赋值有效,但是为什么呢?你知道吗
目前没有回答
相关问题 更多 >
编程相关推荐