【问题标题】:How to use a lambda function with an if-else statement in tkinter如何在 tkinter 中使用带有 if-else 语句的 lambda 函数
【发布时间】:2021-05-14 16:08:05
【问题描述】:

您好,我是 Python 和 tkinter 的初学者,我需要使用一个 Button,根据用户通过 Entry 引入的键向我显示字典中的值。如果引入的键在字典中,则该值应显示在已禁用条目中以描述键,如果它不在字典中,则应显示一个消息框,要求输入不同的键。我在按钮的命令中使用了 if-else lambda 函数,但它似乎不起作用,我不知道为什么。

我将把我正在使用的代码放在下面。

from tkinter import *
from tkinter import messagebox


root=Tk()

dictio={1:"one", 2:"two"}

test=False
textEntry = StringVar()
def show_description(key, dict, text):
    if key in dict.keys():
        textEntry.set(text)
    else:
        test=True

code_entry = Entry(root)
code_entry.grid(row=1, column=1, sticky='nsew')
description_entry = Entry(root, state="disabled", textvariable = textEntry)
description_entry.grid(row=0, column=1, sticky='nsew')

show_button = Button(root, text="Mostrar descripción", command=lambda test:show_description(int(code_entry.get()),
                                                                                dictio,
                                                                                dictio[int(code_entry.get())]) if (test==False) else messagebox.showinfo("Info", "The number is not in the database"))


show_button.grid(row=0, column=2)
root.mainloop()

【问题讨论】:

  • 只定义一个普通的python函数。它将使您的代码更具可读性/可调试性。
  • @TheLizzard 但使用普通函数我无法在按钮命令中分配参数,这就是我使用 lambda 的原因。
  • 当然可以。定义def foo(test): ...,然后使用Button(root, text="...", command=foo)
  • 如果你创建一个合适的函数而不是把代码塞进一个复杂的 lambda 中,你的代码会更容易编写、阅读和管理。
  • 按钮的command 选项的回调应该没有参数,所以你的lambda(带有test 参数)应该会导致错误。

标签: python tkinter lambda messagebox


【解决方案1】:

command 参数需要一个没有位置参数的函数,因此使用 lambda x: <do-something> 会引发错误。在这种情况下,不需要在回调期间传递任何参数,因此您应该将事情简化为

def show_description():
    key = int(code_entry.get())
    if key in dictio:
        textEntry.set(dictio[key])
    else:
        messagebox.showinfo("Info", "The number is not in the database")

show_button = Button(root, text="Mostrar descripción", command=show_description)

还有,这样做

dictio[int(code_entry.get())]

在修复没有参数的 lambda 之后,您的做法可能会引发 KeyError

【讨论】:

  • 谢谢!现在我明白了,我只是让代码过于复杂。
【解决方案2】:
from tkinter import *
from tkinter import messagebox


root=Tk()

dictio={1:"one", 2:"two"}

test=False
textEntry = StringVar() 
display=StringVar()
def show_description():
    print(display.get()) #==get the value of the stringvat
    x=int(display.get()) #==convert the value to integer
    if dictio.get(x)==None:
        messagebox.showinfo("Info", "The number is not in the database")
    else:
        textEntry.set(dictio.get(x)) #==get the value of the key from dictio dictionary and set it for description_entry
code_entry = Entry(root,textvariable=display)
display.set("") #==set value as nothing
code_entry.grid(row=1, column=1, sticky='nsew')
description_entry = Entry(root, state="disabled", textvariable = textEntry)
description_entry.grid(row=0, column=1, sticky='nsew')

show_button = Button(root, text="Mostrar descripción", command=show_description)


show_button.grid(row=0, column=2)
root.mainloop()

【讨论】:

  • OP 的代码有一个messagebox.showinfo(...)。它去哪儿了?您还介意对您所做的更改/它的工作原理添加一些解释吗?
  • 这里不鼓励仅使用代码的答案。如果您描述您对代码所做的更改,此答案将更加有用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-20
  • 1970-01-01
相关资源
最近更新 更多