【问题标题】:Make a dictionary using tkinter, GUI使用 tkinter、GUI 制作字典
【发布时间】:2016-03-18 02:16:21
【问题描述】:

我想使用 GUI 制作字典,我正在考虑制作两个条目,一个用于对象,另一个用于键。我想制作一个按钮来执行信息并将其添加到空字典中。

from tkinter import *

fL = {}

def commando(fL):
    fL.update({x:int(y)})


root = Tk()
root.title("Spam Words")

label_1 = Label(root, text="Say a word: ", bg="#333333", fg="white")
label_2 = Label(root, text="Give it a value, 1-10:", bg="#333333", fg="white")
entry_1 = Entry(root, textvariable=x)
entry_2 = Entry(root, textvariable=y)

label_1.grid(row=1)
label_2.grid(row=3)

entry_1.grid(row=2, column=0)
entry_2.grid(row=4, column=0)

but = Button(root, text="Execute", bg="#333333", fg="white", command=commando)
but.grid(row=5, column=0)

root.mainloop()

我想稍后在我的主程序中使用该字典。你看看它是否是一个函数,我会进入 IDLE 并执行..

 def forbiddenOrd():

        fL = {}
        uppdate = True
        while uppdate:
            x = input('Object')
            y = input('Key')
            if x == 'Klar':
                break
            else:
                fL.update({x:int(y)})
        return fL

然后在我的程序中进一步使用该功能 有什么建议? 我很感激。谢谢

【问题讨论】:

    标签: python dictionary input tkinter


    【解决方案1】:

    您已接近实现您想要的目标。需要进行一些修改。首先,让我们从输入框entry_1entry_2 开始。像您一样使用text variable 是一个好方法;但是我没有看到它们被定义,所以它们在这里:

    x = StringVar()
    y = StringVar()
    

    接下来,我们需要更改调用commando 函数的方式以及通过它传递的参数。虽然我想传递xy 值,但我不能只使用command=commando(x.get(), y.get()) 之类的东西来做到这一点,我需要使用lambda,如下所示:

    but = Button(root, text="Execute", bg="#333333", fg="white", command=lambda :commando(x.get(), y.get()))
    

    现在为什么我将值 xy 传递为 x.get()y.get()?为了从 tkinter 变量(例如 xy)中获取值,我们需要使用 .get()

    最后,让我们修复commando 函数。您不能像使用 fL 作为参数那样使用它。这是因为您在此处设置的任何参数都会成为该函数的私有变量,即使它出现在您的代码中的其他位置。换句话说,将函数定义为def commando(fL): 将阻止函数外部的fL 字典在commando 内进行评估。你如何解决这个问题?使用不同的参数。由于我们将xy 传递到函数中,因此我们将它们用作参数名称。这就是我们的函数现在的样子:

    def commando(x, y):
        fL.update({x:int(y)})
    

    这将在您的字典中创建新项目。这是完整的代码:

    from tkinter import *
    
    fL = {}
    
    def commando(x, y):
        fL.update({x:int(y)})  # Please note that these x and y vars are private to this function.  They are not the x and y vars as defined below.
        print(fL)
    
    root = Tk()
    root.title("Spam Words")
    
    x = StringVar()  # Creating the variables that will get the user's input.
    y = StringVar()
    
    label_1 = Label(root, text="Say a word: ", bg="#333333", fg="white")
    label_2 = Label(root, text="Give it a value, 1-10:", bg="#333333", fg="white")
    entry_1 = Entry(root, textvariable=x)
    entry_2 = Entry(root, textvariable=y)
    
    label_1.grid(row=1)
    label_2.grid(row=3)
    
    entry_1.grid(row=2, column=0)
    entry_2.grid(row=4, column=0)
    
    but = Button(root, text="Execute", bg="#333333", fg="white", command=lambda :commando(x.get(), y.get()))  # Note the use of lambda and the x and y variables.
    but.grid(row=5, column=0)
    
    root.mainloop()
    

    【讨论】:

    • 我明白了!卡蒙,非常感谢!然而,在我们关闭此页面之前,我有一个问题。你为什么放 lambda?它有什么特别之处?我的意思是我在 YouTube 上看过一些视频,但我们为什么在这种情况下使用它?我们已经定义了一个函数,但我们仍然使用 lambda 吗?
    • @MajdJamal 如果您需要将参数传递给函数,在 tkinter 应用程序(和其他 GUI 工具)中使用 lambda 特别有用。如果您使用command=function(param1, param2) 之类的东西,则应用程序一启动,该命令就会立即运行(尽管用户没有按下按钮)。在command= 中使用lambda,该函数将仅在按下按钮时使用我们给定的参数运行。我建议您尝试删除 lambda,看看会发生什么。希望这能把事情弄清楚一点。
    猜你喜欢
    • 2021-03-27
    • 2021-11-23
    • 1970-01-01
    • 1970-01-01
    • 2012-04-10
    • 2016-09-18
    • 2023-04-01
    • 1970-01-01
    • 2023-01-21
    相关资源
    最近更新 更多