user12636845
user12636845

Reputation:

How To Add Elements To a Dictionary?

dam trying to make a phonebook project using python and Tkinter I need to add phone numbers to my phone book and i have written code for that but python throws out error.

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

This is my code and when i execute this

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

python shows the above error , What to do?

Upvotes: 0

Views: 128

Answers (3)

Gopinath
Gopinath

Reputation: 4937

Here is the working solution:

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

Output:

$ 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: Screenshots of the phone book windows

Upvotes: 0

tobias_k
tobias_k

Reputation: 82899

While you use global my_dict when you define my_dict in check, it will still be only defined after you call check. And indeed, if you invoke check first, and then try to add a number, it works.

Define my_dict in the global scope, together with root etc., then it will work regardless of in which order the buttons are used.

my_dict = {"john":'7598769587'}
root = Tk()
...

(Then you can also remove the global my_dict from check as it will only read the variable.)

Upvotes: 1

Phoenixo
Phoenixo

Reputation: 2113

I think you just need to set my_dict as global in your submit() function

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

Upvotes: 0

Related Questions