很简单的电话簿
import easygui as guiimport pickle
import os
def chart():
choice = ["1.添加联系人","2.删除联系人","3.查找联系人","4.打印所有","5.退出程序"]
reply = gui.choicebox("欢迎使用本程序,请选择您所需要的功能:",choices=choice)
return reply
def add_contacts():
fields=['姓名:','联系方式:']
values=gui.multenterbox(msg="1.添加联系人",fields=fields)
name = values
numbers = values
a = numbers
def del_contacts():
fields=['姓名:']
values=gui.multenterbox(msg="2.删除联系人",fields=fields)
name = values
a.pop(name)
def find_contacts():
name = gui.enterbox(msg="请输入待查找的联系人姓名:")
x = a.get(name)
if (x):
gui.msgbox(msg='姓名: %s \n联系方式:%s'%(name,x),title='联系人详细信息')
else:
gui.msgbox(msg='您所查找的联系人不存在')
def save():
f = open("a.pkl",'wb')
pickle.dump(a,f)
f.close()
def read():
f = open("a.pkl",'rb')
a = pickle.load(f)
f.close()
return a
def print_all():
choice = []
for eachkey in a.keys():
choice.append(eachkey)
name = gui.choicebox(msg='所有联系人:',choices=choice)
x = a.get(name)
if (x):
gui.msgbox(msg='姓名: %s \n联系方式:%s'%(name,x),title='联系人详细信息')
else:
gui.msgbox(msg='您所查找的联系人不存在')
while (1):
all_files = os.listdir(os.curdir)
if ("a.pkl" in all_files):
a = read()
else:
a = {}
reply = chart()
if (reply == '1.添加联系人'):
add_contacts()
save()
a = read()
print_all()
elif (reply == '2.删除联系人'):
del_contacts()
save()
a = read()
print_all()
elif (reply == '3.查找联系人'):
find_contacts()
elif (reply == '4.打印所有'):
print_all()
elif (reply == '5.退出程序'):
save()
break
页:
[1]