简体   繁体   中英

Make a dictionary using tkinter, GUI

I want to make a dictionary by using a GUI, I was thinking of making two entries, one for the object and the other for the key. And I want to make a button that execute the information and add it to the empty dictionary.

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()

I want to use that dictionary later in my main program. You see if it would be a function, I would just go in IDLE and do..

 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

And then just use the function further on in my program Any suggestions? I appreciate it. Thank you

You are close to achieving what you want. There are a few modifications that need to be made. First, lets start with the entry boxes entry_1 and entry_2 . Using a text variable like you did is a good approach; however I did not see them defined, so here they are:

x = StringVar()
y = StringVar()

Next, we need to change how you call the commando function and what parameters you pass though it. I want to pass the x and y values though, but I can't do this by just using something like command=commando(x.get(), y.get()) , I need to use lambda as follows:

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

Now why did I pass the values x and y as x.get() and y.get() ? In order to get the values from a tkinter variable such as x and y , we need to use .get() .

Finally, let's fix the commando function. You cannot use it as you did with fL being the parameter. This is because any parameter you set there becomes a private variable to that function even if it appears elsewhere in you code. In other words, defining a function as def commando(fL): will prevent the fL dictionary outside the function from being assessed within commando . How do you fix this? Use different parameters. Since we are passing x and y into the function, let's use those as parameter names. This is how our function looks now:

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

This will create new items in your dictionary. Here is the completed code:

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM