如何让 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=">", command=lambda: current_instance.modify_attr(b, 1))
button.place(x=120, y=80 + 18 * b)
目标是生成两列按钮并将每个按钮对绑定(bind)到一对(有点复杂的)函数(有条件的 reduce_by_one/increase_by_one 函数)。
- x = 我需要生成的按钮对的数量
- current_instance = 类实例
- modify_attr = 一个接受两个参数的类方法(如果我们包含 self,我猜是三个)
我的理解(基于 this 和我在过去几年阅读的其他内容)是这个问题很常见。从本质上讲,问题是关于 modify_attr() 的所有 b 值最终都等于 len(x)
(而不是此时 b 的值我打算将该命令绑定(bind)到按钮)。结果是一系列正确定位的按钮(通过 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 很有魅力。现在,在这种情况下,我绑定(bind)的是热键而不是按钮命令,但我根本无法理解为什么它会以不同的方式工作。因为从根本上说,问题出在 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, text="<", command=lambda: helper_lambda(b, -1))
button.place(x=110, y=80 + 18 * b)
button = tk.Button(button_frame, text=">", command=lambda: helper_lambda(b, 1))
button.place(x=120, 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=">", command=helper_lambda(b, 1))
button.place(x=150, y=80 + 18 * b)
但是,您可以在没有辅助函数的情况下直接使用 lambda
:
for b in range(x):
button = tk.Button(button_frame, text="<", command=lambda b=b: current_instance.modify_attr(b, -1))
button.place(x=110, y=80 + 18 * b)
button = tk.Button(button_frame, text=">", command=lambda b=b: current_instance.modify_attr(b, 1))
button.place(x=150, y=80 + 18 * b)
关于python - 与词法作用域和 for 循环作斗争,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65783935/