gpt4 book ai didi

python - 使用 Tkinter 的 'command = ' 中的 lambda 函数。

转载 作者:太空宇宙 更新时间:2023-11-03 13:16:21 25 4
gpt4 key购买 nike

这是一段非常容易理解的代码:

主要:

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)

当我运行 main 时,我收到以下错误:

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

在处理上述异常的过程中,又发生了一个异常:

追溯(最近的调用最后): 文件“/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/tkinter/init.py”,第 1533 行,在 call 返回 self.func(*args)TypeError: () 缺少 3 个必需的位置参数:“evt”、“Current_Weight”和“entree1”

我该如何解决?

我认为 lambda 函数允许我们在事件相关函数中使用一些参数。

最佳答案

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)

关于python - 使用 Tkinter 的 'command = ' 中的 lambda 函数。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28779395/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com