马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 Estein 于 2016-9-18 23:46 编辑 # Mydictionary
# Versin:2.0
# Author:Estein
# Date:2016/9/18
import easygui as g
import pickle
import sys
def load_data(filename):
f = open(filename,'rb')
Mydict = pickle.load(f)
f.close()
return Mydict
def save_data(filename,Mydict):
f = open(filename,'wb')
pickle.dump(Mydict,f)
f.close()
def welcome():
msg = '欢迎使用My Dictionary!'
title = 'Dictionary'
ok_button = '进入'
flag = g.msgbox(msg,title,ok_button)
return flag
def menu():
msg = '选择待执行指令:'
title = '菜单'
Button1 = '显示字典'
Button2 = '添加单词'
Button3 = '删除单词'
Button4 = '查询单词'
Button5 = '退出'
choice = g.buttonbox(msg,title,choices = (Button1,Button2,Button3,Button4,Button5))
return choice
def display(Mydict):
msg = '字典内容如下:'
title = '字典显示...'
text = ''
for each in Mydict:
text += each + ' : ' + Mydict[each] + '\n'
g.textbox(msg,title,text)
def add_word(Mydict,values = ()):
msg = '输入想添加入字典的单词及释义:'
title = '正在添加单词...'
fields = ('单词','释义')
vals = g.multenterbox(msg,title,fields,values)
try:
if vals[0] == '' or vals[1] == '':
add_word(Mydict,values = [vals[0],vals[1]])
else:
Mydict[vals[0]] = vals[1]
g.msgbox('单词%s已成功加入字典!' % vals[0])
except TypeError:
pass
return Mydict
def del_word(Mydict):
msg = '输入想要删除的单词:'
title = '正在删除单词...'
word = g.enterbox(msg,title)
if word == None:
pass
elif word == '':
del_word(Mydict)
elif word in Mydict:
del Mydict[word]
g.msgbox('单词%s已从字典中移除!' % word)
else:
g.msgbox('单词%s在字典中并不存在!' % word)
return Mydict
def search(Mydict):
msg = '待查询的英文单词:'
title = '查询单词'
word = g.enterbox(msg,title)
if word == None:
pass
elif word == '':
g.msgbox('您并未输入单词,请重新输入!','Warning','确定')
search(Mydict)
elif word in Mydict:
msg1 = word + '的释义如下:'
title1 = title + ':' + word
text1 = Mydict[word]
g.textbox(msg1,title1,text1)
else:
try:
choice = g.buttonbox(word + '在字典中并不存在,是否加入字典?',choices = ('Yes','No'))
if choice == 'Yes':
add_word(Mydict,values = [word,])
else:
pass
except TypeError:
pass
def main():
filename = 'Mydict_binary.pkl'
try:
Mydict = load_data(filename)
except FileNotFoundError:
Mydict = dict()
save_data(filename,Mydict)
flag = welcome()
if flag == None:
sys.exit()
while True:
choice = menu()
if choice == '显示字典':
display(Mydict)
if choice == '添加单词':
Mydict = add_word(Mydict)
save_data(filename,Mydict)
if choice == '删除单词':
Mydict = del_word(Mydict)
save_data(filename,Mydict)
if choice == '查询单词':
search(Mydict)
if choice == None or choice == '退出':
sys.exit()
main()
easygui的字体比较丑,显示字位置居中不会调,界面待优化,欢迎界面优化的意见~~ |