+-

这是一个非常容易理解的代码:
主要:
import pdb
#pdb.set_trace()
import sys
import csv
sys.version_info
if sys.version_info[0] < 3:
from Tkinter import *
else:
from tkinter import *
from Untitled import *
main_window =Tk()
main_window.title("Welcome")
label = Label(main_window, text="Enter your current weight")
label.pack()
Current_Weight=StringVar()
Current_Weight.set("0.0")
entree1 = Entry(main_window,textvariable=Current_Weight,width=30)
entree1.pack()
bouton1 = Button(main_window, text="Enter", command= lambda evt,Current_Weight,entree1: get(evt,Current_Weight,entree1))
bouton1.pack()
在另一个文件Untitled我有“获取”功能:
def get (event,loot, entree):
loot=float(entree.get())
print(loot)
当我运行主时我收到以下错误:
Exception in Tkinter callback
Traceback (most recent call last):
File “/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/idlelib/run.py”, line 121, in main
seq, request = rpc.request_queue.get(block=True, timeout=0.05)
File “/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/queue.py”, line 175, in get
raise Empty
queue.Empty
在处理上述异常期间,发生了另一个异常:
Traceback(最近一次调用最后一次):
文件“/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/tkinter/init.py”,第1533行,正在通话中
return self.func(* args)
TypeError :()缺少3个必需的位置参数:’evt’,’Current_Weight’和’entree1′
我怎么解决这个问题?
我认为lambda函数允许我们在依赖于事件的函数中使用一些args.
最佳答案
命令lambda根本不接受任何参数;此外,没有你能抓住的东西. lambda可以引用它之外的变量;这被称为闭包.因此,您的按钮代码应为:
bouton1 = Button(main_window, text="Enter",
command = lambda: get(Current_Weight, entree1))
你应该说:
def get(loot, entree):
loot = float(entree.get())
print(loot)
点击查看更多相关文章
转载注明原文:python – 在Tkinter的’command =’中使用lambda函数. - 乐贴网