|
发表于 2021-4-19 11:58:27
|
显示全部楼层
import easygui as g;
import os;
import sys;
import pickle as p;
os.chdir(os.curdir);
filepath = g.fileopenbox(msg='打开一个二进制文件,存在就选择他,不存在就取消',title='通讯录');
print(filepath);
#with open(filepath,'rb') as f:
try:
with open(filepath,'rb') as f :
try:
fa = p.load(f);
f.close;
except EOFError:
fa = {};
except TypeError:
fa ={};
print('-|-a');
filepath='通讯录.pkl';
TongxunData = fa;
fs = open(filepath,'wb');
uersattribut = ['真实是姓名', '电话号码', 'QQ号码', '电子邮件'];
def Findname(name,zidian={}):
lisnamedata = zidian[name];
userdata = name+'的联系资料'
g.multenterbox(msg=userdata,title='通讯录',fields=uersattribut,values=lisnamedata)
#print('''
#|username | :%s|
#|reallyname | :%s|
#|telephonenumb| :%s|
#|qqwords | :%s|
#|e_mailwords | :%s|'''%(name,lisnamedata[0],lisnamedata[1],lisnamedata[2],lisnamedata[3]));
return
def Addname(filea,newname=str(),zidian={}):
#reallyname = input('请输入真是姓名\n');
#telephonenumb = input('请输入电话号码\n');
#qqwords = input('请输入QQ号码\n');
#e_mailwords = input('请输入你的电子邮件地址\n');
#peopledata = [reallyname,telephonenumb,qqwords,e_mailwords];
peopledata = g.multenterbox(msg='联系人资料',title='通讯录',fields=uersattribut);
zidian[newname] = peopledata;
p.dump(zidian,filea);
filea.flush();
return
def Modifynamedata(name,zidian={}):
#dataname =['reallyname','telephonenumb','qqwords','e_mailwords'];
#dataname = ['真实是姓名', '电话号码', 'QQ号码', '电子邮件'];
olddataname = zidian[name];
newdataname = g.multenterbox(msg='联系人资料',title='通讯录',fields=uersattribut,values=olddataname);
data=[]
for i in range(0,len(olddataname)):
#msga='你想修改%s的%s吗(yes/no)'%(name,dataname[i]);
#if g.buttonbox(msg=msga,title='通讯录',choices=['Yes','No']) == 'yes':
if olddataname[i] == newdataname[i]:
#msgb = "请输入你的%s:"%dataname[i];
#newelement = g.enterbox(msg=msgb,title='通讯录');
data.append(olddataname[i]);
else:
data.append(newdataname[i]);
zidian[name]=data;
return
def Caozuo(datatelephon={}):
while True:
try:
#order = int(input('请输入命令find(1)add(2),modify(3)remove(4)esc(5)\n'));
order = g.buttonbox(msg='请操作',title='通讯录',choices=['查找联系人','添加联系啊人','修改联系人资料','删除联系人','退出']);
if order == '查找联系人':
gvename = g.enterbox(msg='请输入用户名',title='通讯录');
if gvename in datatelephon.keys():
Findname(gvename, datatelephon);
continue
if order == '添加联系啊人':
gvename = g.enterbox(msg='请输入用户名',title='通讯录');
if gvename in datatelephon.keys():
Findname(gvename, datatelephon);
continue;
else:
gvename = gvename;
Addname(fs,gvename, datatelephon);
continue;
if order == '修改联系人资料':
gvename = g.enterbox(msg='请输入用户名',title='通讯录');
if gvename in datatelephon.keys():
Modifynamedata(gvename,datatelephon);
p.dump(datatelephon, fs);
fs.flush();
continue
if order == '删除联系人':
gvename = g.enterbox(msg='请输入用户名',title='通讯录');
if gvename in datatelephon.keys():
datatelephon.__delitem__(gvename);
p.dump(datatelephon,fs);
fs.flush();
continue;
if order == '退出':
p.dump(datatelephon, fs);
break ;
g.msgbox(msg='没有此用户,请注册新用户',title='通讯录')
#print('没有此用户,请注册新用户')
continue;
except Exception as reason:
g.exceptionbox(reason);
print(datatelephon);
fs.close();
sys.exit();
Caozuo(datatelephon=TongxunData); |
|