挣扎于词法范围和 for 循环

问题描述

如何让 modify_attr() 函数(如下)在以下 for 循环中捕获/更新 b 的值? (简化版,出现在 mainloop() 中):

for b in range(x):

    button = tk.Button(button_frame,text="<",command=lambda: current_instance.modify_attr(b,-1))
    button.place(x=110,y=80 + 18 * b)
    button = tk.Button(button_frame,text=">",1))
    button.place(x=120,y=80 + 18 * b)

目标是生成两列按钮,并将每个按钮对绑定到(有些复杂的)函数对(有条件的 reduce_by_one / increase_by_one 函数)。

我的理解(基于 ​​this 和我在过去阅读的其他内容)是这个问题很常见。从本质上讲,问题在于,关于 modify_attr() 的所有 b 值最终都等于 len(x)(而不是我打算绑定该命令时的 b 值到按钮)。结果是一系列按钮位置正确(通过 button.place 中的 b 值),但都指向列表中它们应该修改的最后一个元素。

我以前遇到过这个确切的问题,并且能够使用辅助函数解决它。但出于某种原因,我无法在此处应用该解决方案(为了清楚起见,再次简化):

for b in range(len(the_list)):
    def helper_lambda(c):
        return lambda event: refresh_frame(the_list[c])
    window.bind(b + 1,helper_lambda(b))

有意义吗?完全相同的问题, helper_lamdba 就像一个魅力。现在,在这种情况下,我绑定的是热键而不是按钮命令,但我根本无法理解为什么它会以不同的方式工作。因为从根本上说问题出在 for 循环上,而不是里面的函数。但是当我在我的按钮循环中实现一个辅助函数时,它就像一个没有魅力的人一样失败了。

这是我尝试应用该辅助策略失败的尝试:

for b in range(x):
    def helper_lambda(c,modifier):
        return lambda event: current_instance.modify_attr(c,modifier)

    button = tk.Button(button_frame,command=lambda: helper_lambda(b,y=80 + 18 * b)

我做错了什么?另外,为什么它会这样?有人在 for 循环之外使用增量值吗?!

解决方法

第二种方法可能只需稍作改动即可:

for b in range(x):
    def helper_lambda(c,modifier):
        return lambda: current_instance.modify_attr(c,modifier)  # removed event argument

    button = tk.Button(button_frame,text="<",command=helper_lambda(b,-1))
    button.place(x=110,y=80 + 18 * b)
    button = tk.Button(button_frame,text=">",1))
    button.place(x=150,y=80 + 18 * b)

但是,您可以直接使用 lambda 而无需辅助函数:

for b in range(x):
    button = tk.Button(button_frame,command=lambda b=b: current_instance.modify_attr(b,y=80 + 18 * b)
,

在这种情况下,functools.partial 是比 lambda 表达式更好的选择。

from functools import partial

for b in range(x):

    button = tk.Button(button_frame,command=partial(current_instance.modify_attr,b,1))
    button.place(x=120,y=80 + 18 * b)

partial 接收 bvalue 作为参数,而不是简单地捕获 name b 供以后使用。