这是一个非常容易理解的代码:
主要:
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()
在另一个文件中无标题我有“获取”功能:
def get (event,loot, entree):
loot=float(entree.get())
print(loot)
当我运行主时,我收到以下错误:
Tkinter回调中的异常 Traceback(最近一次调用最后一次): 文件“/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/idlelib/run.py”,第121行,主要 seq,request = rpc.request_queue.get(block = True,timeout = 0.05) 文件“/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/queue.py”,第175行,获取 提高空 queue.Empty
在处理上述异常期间,发生了另一个异常:
追踪(最近一次通话): 文件“/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。
答案 0 :(得分:7)
command
lambda根本不接受任何争论;此外,你没有evt
可以捕获。 lambda可以引用它之外的变量;这被称为闭包。因此,您的按钮代码应为:
bouton1 = Button(main_window, text="Enter",
command = lambda: get(Current_Weight, entree1))
你的get
应该说:
def get(loot, entree):
loot = float(entree.get())
print(loot)
答案 1 :(得分:0)
实际上,您只需要Entry对象entree1作为lamda传入参数。以下任何一种说法都可以。
bouton1 = Button(main_window, text="Enter", command=lambda x = entree1: get(x))
bouton1 = Button(main_window, text="Enter", command=lambda : get(entree1))
该函数的定义为
def get(entree):
print(float(entree.get()))