Python 酸洗字典数据,然后加载它不会';行不通
我正在制作一份客户名单,并使用字典和pickle保存和存储客户姓名和年龄。不幸的是,我的save函数会覆盖所有现有用户,然后我的Python 酸洗字典数据,然后加载它不会';行不通,python,dictionary,tkinter,pickle,Python,Dictionary,Tkinter,Pickle,我正在制作一份客户名单,并使用字典和pickle保存和存储客户姓名和年龄。不幸的是,我的save函数会覆盖所有现有用户,然后我的\uuuu init\uuuu函数不会在字典中显示pickle数据,而只是我自己键入的数据。另外,add函数不会显示带有客户机名称的标签。你能帮我解决泡菜问题吗?因为我需要一个快速的答案 提前谢谢 from Tkinter import * import pickle root=Tk() dic={} class Launch(object): def __
\uuuu init\uuuu
函数不会在字典中显示pickle数据,而只是我自己键入的数据。另外,add
函数不会显示带有客户机名称的标签。你能帮我解决泡菜问题吗?因为我需要一个快速的答案
提前谢谢
from Tkinter import *
import pickle
root=Tk()
dic={}
class Launch(object):
def __init__(self, root):
root.withdraw()
root=Toplevel(root)
root.title("Choose Client")
self.row=3
for key in dic:
Label(root, text=key).grid(row=row, column=0)
self.row+=1
l3=Label(root, text="Client Name")
l3.grid(row=0, column=0)
self.e1=Entry(root)
self.e1.grid(row=0, column=1)
l4=Label(root, text="Client age")
l4.grid(row=1, column=0)
self.e2=Entry(root)
self.e2.grid(row=1, column=1)
b3=Button(root, text="Create client", command=self.add)
b3.grid(row=2)
def add(self):
client=self.e1.get()
age=self.e2.get()
dic[client]=age
Label(root, text="%s" % (client)).grid(row=self.row)
with open("data", "w") as f:
pickle.dump(dic, f)
def load(self):
dic=pickle.load(open("data", "rb"))
app=Launch(root)
root.mainloop()
根本不使用
load
方法。以下是使用加载
修改的代码
from Tkinter import *
import pickle
root=Tk()
dic={}
class Launch(object):
def __init__(self, root):
self.load() # <-- load
root.title("Choose Client")
self.row = 3
for key in dic:
Label(root, text=key).grid(row=self.row, column=0)
self.row += 1
l3=Label(root, text="Client Name")
l3.grid(row=0, column=0)
self.e1=Entry(root)
self.e1.grid(row=0, column=1)
l4=Label(root, text="Client age")
l4.grid(row=1, column=0)
self.e2=Entry(root)
self.e2.grid(row=1, column=1)
b3=Button(root, text="Create client", command=self.add)
b3.grid(row=2)
def add(self):
client=self.e1.get()
age=self.e2.get()
dic[client]=age
Label(root, text=client).grid(row=self.row)
self.row += 1 # <--- increase row count
with open("data", "wb") as f:
pickle.dump(dic, f)
def load(self):
# should be declared, otherwise, will create local variable
global dic
try:
dic = pickle.load(open("data", "rb"))
except IOError: # if file does not exist.
pass
app = Launch(root)
root.mainloop()
从Tkinter导入*
进口泡菜
root=Tk()
dic={}
类启动(对象):
定义初始化(自,根):
self.load()#根本不使用load
方法。以下是使用加载
修改的代码
from Tkinter import *
import pickle
root=Tk()
dic={}
class Launch(object):
def __init__(self, root):
self.load() # <-- load
root.title("Choose Client")
self.row = 3
for key in dic:
Label(root, text=key).grid(row=self.row, column=0)
self.row += 1
l3=Label(root, text="Client Name")
l3.grid(row=0, column=0)
self.e1=Entry(root)
self.e1.grid(row=0, column=1)
l4=Label(root, text="Client age")
l4.grid(row=1, column=0)
self.e2=Entry(root)
self.e2.grid(row=1, column=1)
b3=Button(root, text="Create client", command=self.add)
b3.grid(row=2)
def add(self):
client=self.e1.get()
age=self.e2.get()
dic[client]=age
Label(root, text=client).grid(row=self.row)
self.row += 1 # <--- increase row count
with open("data", "wb") as f:
pickle.dump(dic, f)
def load(self):
# should be declared, otherwise, will create local variable
global dic
try:
dic = pickle.load(open("data", "rb"))
except IOError: # if file does not exist.
pass
app = Launch(root)
root.mainloop()
从Tkinter导入*
进口泡菜
root=Tk()
dic={}
类启动(对象):
定义初始化(自,根):
self.load()#如果不继续阅读,您pickle的所有内容都必须是可散列的……它们是吗?load
方法根本不被使用。如果不继续阅读,您pickle的所有内容都必须是可散列的……它们是吗?load
方法根本不被使用。