如何在字典中添加元素?

时间:2020-02-07 17:39:52

标签: python tkinter

试图用python和Tkinter制作电话簿项目的人,我需要在电话簿中添加电话号码,并且我已经为此编写了代码,但是python抛出了错误。

from tkinter import*
#defining the submit button
def submit():
    new = entry2.get()
    new2 = entry3.get()
    my_dict.update({new:new2})

#definig the add button
def add():
top = Toplevel(root)
top.configure(background="black")
top.geometry("400x400")
top.title("Add new member")
label6 =Label(top,text="Welcome",bg="black",fg="orange")
label6.place(x=135,y=13)
global entry2
entry2 = Entry(top,width=23)
entry2.place(x=102,y=78)
label7 = Label(top,text="Name",bg="black",fg="orange")
label7.place(x=48,y=78)
global entry3
entry3 = Entry(top,width = 23)
entry3.place(x=108,y=127)
label8 = Label(top,text="phone number",bg ="black",fg="orange")
label8.place(x=0,y=129)
button3 = Button(top,text="Submit",command = submit)
button3.place(x=185,y=200)
#defining the chek button
def check():
global my_dict

my_dict = {"john":'7598769587'}
if entry.get() in my_dict: 
   label4 = Label(root,text=my_dict[entry.get()],bg="black",fg="orange").place(x=178,y=167)
   label5 = Label(root,text = entry.get()+" is :",bg ="black",fg = "orange").place(x=120,y=167)

   #creating the main window  
   root = Tk()
   root.title("vole phone book")
   root.geometry("400x400")
   root.configure(background="black")
   label = Label(root,text="phone book",bg="black",fg="orange",width=13).place(x=133,y=23)
   label2 = Label(root,text="Enter here.",bg = "black",fg="orange").place(x=2,y=89)
   entry = Entry(root,width = 27)
   entry.place(x=89,y=90)
   button =Button(root,text="Check",bg="yellow",fg="red",command=check).place(x=190,y=129)
   button2=Button(root,text="Add",width=23,command = add).place(x=120,y=300)

   root.mainloop()

这是我的代码,当我执行此代码

    my_dict.update({new:new2})
    NameError: name 'my_dict' is not defined

python显示上述错误,该怎么办?

3 个答案:

答案 0 :(得分:1)

虽然您在<TextField inputRef={register} label={"pincode"} variant="outlined" placeholder={"dd"} multiline={true} rows="4" rowsMax="12" name={"pincode"} onBlur={v => { console.log(v); function onlyUnique(value, index, self) { return self.indexOf(value) === index && value.trim().length === 6; } if (v) { let codes = v.split(","); let newCodes = codes.filter(onlyUnique); return newCodes.sort().join(","); } return ""; }} /> 中定义global my_dict时使用了my_dict,但是在您调用check 之后,它仍然只会被定义为。确实,如果您先调用check,然后再尝试check一个数字,那么它将起作用。

在全局范围内定义add以及my_dict等,然后不管按钮的使用顺序如何,它都将起作用。

root

(然后,您也可以从检查中删除my_dict = {"john":'7598769587'} root = Tk() ... ,因为它只会读取变量。)

答案 1 :(得分:0)

我认为您只需要在my_dict函数中将submit()设置为全局

def submit():
    global my_dict
    new = entry2.get()
    new2 = entry3.get()
    my_dict[new] = new2

答案 2 :(得分:0)

这是可行的解决方案:

# File name: phone-book-demo.py

from tkinter import*

# Define global phone book
global my_dict
my_dict = {"john":'7598769587'}

#defining the submit button
def submit():
    person = entry2.get()
    phone = entry3.get()
    my_dict.update({person:phone})
    print(my_dict)

#definig the add button
def add():
    top = Toplevel(root)
    top.configure(background="black")
    top.geometry("400x400")
    top.title("Add new member")
    label6 =Label(top,text="Welcome",bg="black",fg="orange")
    label6.place(x=135,y=13)

    global entry2
    entry2 = Entry(top,width=23)
    entry2.place(x=102,y=78)
    label7 = Label(top,text="Name",bg="black",fg="orange")
    label7.place(x=48,y=78)

    global entry3
    entry3 = Entry(top,width = 23) 
    entry3.place(x=108,y=127)
    label8 = Label(top,text="phone number",bg ="black",fg="orange")
    label8.place(x=0,y=129)
    button3 = Button(top,text="Submit",command = submit)
    button3.place(x=185,y=200)

#defining the chek button
def check():
    person = entry.get()
    if person in my_dict: 
        phone = my_dict.get(person)
        print("Found: " + person + " : " + phone)
        # Erase the old result by placing a blank label
        label0 = Label(root, width=200, bg ="black", fg = "black").place(x=10,y=167)
        label5 = Label(root, text = person + " : " + phone, bg ="black", fg = "orange").place(x=10,y=167)

#creating the main window  
root = Tk()
root.title("vole phone book")
root.geometry("400x400")
root.configure(background="black")
label = Label(root,text="phone book",bg="black",fg="orange",width=13).place(x=133,y=23)
label2 = Label(root,text="Enter here.",bg = "black",fg="orange").place(x=2,y=89)
entry = Entry(root,width = 27)
entry.place(x=89,y=90)
button =Button(root,text="Check",bg="yellow",fg="red",command=check).place(x=190,y=129)
button2=Button(root,text="Add",width=23,command = add).place(x=120,y=300)
root.mainloop()

输出:

$ python3 phone-book-demo.py 
Found: john : 7598769587
{'john': '7598769587', 'Alice': '123'}
{'john': '7598769587', 'Alice': '123', 'Bob': '777'}
Found: Alice : 123
Found: Bob : 777

电话簿窗口的屏幕截图Screenshots of the phone book windows