python自制简单字典
本帖最后由 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 + '\n'
g.textbox(msg,title,text)
def add_word(Mydict,values = ()):
msg = '输入想添加入字典的单词及释义:'
title = '正在添加单词...'
fields = ('单词','释义')
vals = g.multenterbox(msg,title,fields,values)
try:
if vals == '' or vals == '':
add_word(Mydict,values = ,vals])
else:
Mydict] = vals
g.msgbox('单词%s已成功加入字典!' % vals)
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
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
g.textbox(msg1,title1,text1)
else:
try:
choice = g.buttonbox(word + '在字典中并不存在,是否加入字典?',choices = ('Yes','No'))
if choice == 'Yes':
add_word(Mydict,values = )
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的字体比较丑,显示字位置居中不会调,界面待优化,欢迎界面优化的意见~~ 楼主加油!{:10_298:} 报错,求解 人zai旅途 发表于 2016-9-22 23:05
报错,求解
我的gui框架用的是easygui,要导入这个模块的
Estein 发表于 2016-9-25 22:42
我的gui框架用的是easygui,要导入这个模块的
好的,明白了,还没学习到 人zai旅途 发表于 2016-9-25 22:42
好的,明白了,还没学习到
可以找一下,python的第三方库,包括numpy,tkinter等比较好用的库都有
Estein 发表于 2016-9-25 22:46
可以找一下,python的第三方库,包括numpy,tkinter等比较好用的库都有
嗯嗯,多谢楼主 人zai旅途 发表于 2016-9-25 23:06
嗯嗯,多谢楼主
下载easygui就是进入命令操作符(cmd)输入pip install easygui就行了
页:
[1]