Как сохранить ключ к словарю навсегда? - PullRequest
0 голосов
/ 08 февраля 2020

Я пытаюсь создать телефонную книгу, используя python и tkinter. Мне нужно добавить новые номера в мою телефонную книгу, добавив элементы в словарь, но я не могу получить доступ к номерам после закрытия программы.

from tkinter import*
#defining the submit button
def submit():
  global my_dict
  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)
    button5 = Button(top,text = "close",command = top.quit)
    button5.place(x=185,y=300)
  #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()
 global my_dict
  my_dict = {"john":'7598769587'}
  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()

это мой код, что делать?

1 Ответ

1 голос
/ 08 февраля 2020

Переменные в Python не сохраняются на диск, они существуют только в памяти (RAM), если вы сами не сохраните их там. Вы можете либо сохранить в файл напрямую, либо использовать базу данных.

См. Здесь , чтобы узнать, как использовать Pickle для того, что вы хотите.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...