通过Tkinter中的Button将参数传递给函数,循环中出现starnge行为

蒂莫西·劳曼(Timothy Lawman)

我目前正在尝试修复python书“ Python下一步”中的一个错误,该错误作者尚未修复,并在代码中留下了注释:“稍后修复”

我的第一个解决方案失败了,但是第二个解决方案通过删除了循环成功了。问题是我不知道为什么第一个解决方案失败了!

解决方案1:

当用户使用循环单击Tkinter制造的计算器中的按钮时,Button对象和称为click的函数仅从lambda参数打印大写字母C.在谈论。

解决方案2:

删除生成按钮的循环并重复手动编码每个按钮!这可以正常工作,正如Brian kernighan所建议的那样:首先使它工作,然后再提高效率!

代码:

# myCalculator3_final.py

from Tkinter import *
from decimal import *

# key press function:
def click(key):
        display.insert(END, key)


##### main:
window = Tk()
window.title("MyCalculator")

# create top_row frame
top_row = Frame(window)
top_row.grid(row=0, column=0, columnspan=2, sticky=N)

# use Entry widget for an editable display
display = Entry(top_row, width=45, bg="light green")
display.grid()

# create num_pad_frame
num_pad = Frame(window)
num_pad.grid(row=1, column=0, sticky=W)

# This method of passing an argument to click work! Loop removed and buttons hand code
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#-------------------------------------------------------------------------
# create num_pad buttons passing an argument to the command function click
#-------------------------------------------------------------------------   
seven = Button(num_pad, text="7", width=5, command=lambda :click("7"))
seven.grid(row=0,column=0)
eight = Button(num_pad, text="8", width=5, command=lambda :click("8"))
eight.grid(row=0,column=1)
nine= Button(num_pad, text="9", width=5, command=lambda :click("9"))
nine.grid(row=0,column=2)
four= Button(num_pad, text="4", width=5, command=lambda :click("4"))
four.grid(row=1,column=0)
five= Button(num_pad, text="5", width=5, command=lambda :click("5"))
five.grid(row=1,column=1)
six= Button(num_pad, text="6", width=5, command=lambda :click("6"))
six.grid(row=1,column=2)
one= Button(num_pad, text="1", width=5, command=lambda :click("1"))
one.grid(row=2,column=0)
two= Button(num_pad, text="2", width=5, command=lambda :click("2"))
two.grid(row=2,column=1)
three= Button(num_pad, text="3", width=5, command=lambda :click("3"))
three.grid(row=2,column=2)
zero= Button(num_pad, text="0", width=5, command=lambda :click("0"))
zero.grid(row=2,column=0)
#---------------------------------------------------------------------------   



# calculate the row, column for button

# create operator_frame
operator = Frame(window)
operator.grid(row=1, column=1, sticky=E)

operator_list = [
'*', '/',  
'+', '-',
'(', ')',
'C' ]

# The authors code and I have added  the same lambda function as above but 
#it just prints out capital C
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
r = 0
c = 0
for btn_text in operator_list:
    Button(operator, text=btn_text, width=5, command=lambda: click(btn_text)).grid(row=r,column=c)
    c = c+1
    if c > 1:
        c = 0
        r = r+1


##### Run mainloop
window.mainloop()

问题:

为什么单击lambda调用方法以传递参数无法在循环中起作用,而只显示C,但是如果我删除循环,则它起作用!

凯文

在这行上:

Button(operator, text=btn_text, width=5, command=lambda: click(btn_text)).grid(row=r,column=c)

btn_textLambda中的值不会被其当前值冻结。相反,当btn_text在循环的下一个迭代中更改,它在lambda中求值的值也将更改。这意味着您的所有Button都有效地具有一个click('C')命令,因为'C'是的最终值btn_text

你可以做:

command=lambda text=btn_text: click(text)

或者

command=(lambda text: lambda: click(text))(btn_text)

text将捕获的当前值btn_text,并且此后不会更改。您的命令将使用适当的参数调用。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何通过POST将参数传递给Azure函数?

使用asp:Button将参数传递给函数

celerybeat如何通过调度程序将参数传递给函数?

通过列位置将函数参数传递给mutate_at

Dart:通过变量将命名参数传递给函数

通过HttpTrigger将值/参数传递给Azure Powershell函数

如何从foreach循环php将更多参数传递给函数

将参数传递给PowerShell函数时的不同行为

VBA通过函数将多个参数传递给SQL

Javascript在循环中将参数传递给eventListener函数

通过链接HREF将参数传递给函数

通过javascript中的参数将全局变量传递给函数的用途是什么?

通过值将参数传递给onClick函数

从Tkinter中的Entry小部件将参数传递给动态创建的Button

通过PHP将参数传递给Javascript函数

将变量传递给在for循环中创建的click函数

将参数传递给Ajax中的函数

如何通过服务容器将参数传递给构造函数?

通过反射将参数传递给函数

将参数传递给函数并通过“&”调用它

使用循环将参数传递给函数

将数据 onClick 事件传递给循环中的函数

VBA - 将参数传递给循环中的函数

VBA 将多个参数传递给 for 循环内的函数

Python:在循环中创建 Tkinter 按钮时如何在 lambda 函数中传递不同的参数?

将参数传递给 forEach 循环内的回调函数?

无法将参数传递给循环内的函数调用

传递键以将循环中的值与参数函数进行比较

如何通过 Firebase 函数将参数传递给外部 API