DictForGeeks是个人写的一款词典软件,支持高度自定义。个人在学习过程中,发现很多科技词汇的意思跟主流的词义很不一样就想写一款可以自己定义词义的词典,支持添加和查询功能。至少要比txt记单词要方便。于是便有了这么个小程序。语言使用python,GUI使用的是tkinter,本来想用wxpython的,但是考虑到很多pythoner没有安装wxpython,还是决定用它自带的GUI了。
用法:第一个窗口是单词输入窗口;第二个是提示窗口,支持双击选择;第三个窗口是解释窗口,可以随时编辑;旁边有个“保存”按钮,可以把修改好的保存起来;最下面还有一个提示栏,提示是否保存成功。
# -*- coding: utf-8 -*- """ DictForGeeks is an useful dict tools for the geeks who want to change and save New words and phrases any time.English and Chinese both supported. It is totally free, you can use or modify for any purpose. Write by Python, GUI is Tkinter. Hope you like it. Usage: First it will create a txt file called "DictForGeeksdata" where the source code is. The next time you start this code will read this file as the database for your Dict. author: JiangKun email: [email protected] """ import sys import os from Tkinter import * reload(sys) sys.setdefaultencoding('utf-8') #处理响应函数 #查询窗口的变化的响应函数 def callback(sv): word=entry.get().strip() t1.delete(0.0,END) lbox.delete(0,END) Tip(0) datalist=list(data) datalist.sort() for i in datalist: if i>=word: lbox.insert(END,str(i)) if word in data: t1.insert(INSERT,str(data[word])) elif len(entry.get().strip())!=0: t1.insert(INSERT,str_cannotfind) #解释框改变时,将状态设为空 def TextChange(event): Tip(0) #将Listbox中选中的项加载到查询窗口 def printList(event): lbox_v=lbox.get(lbox.curselection()) e.set(lbox_v) #保存 def Save(): word=entry.get().strip() answer=t1.get(0.0,END).strip() if len(word)==0 or len(answer)==0: Tip(3) elif answer==str_cannotfind: Tip(2) elif word in data: if answer==data[word]: Tip(4) else: data[word]=answer Tip(1) elif word not in data: data[word]=answer Tip(1) SaveInFile() #写入文本文件 def SaveInFile(): s = '' for k in data: s += '%s~%s\n' % (k,data[k]) fout = open(fname,'w') fout.write(s) fout.close() #提示项 def Tip(n): if n==0: tip.set(str_null) if n==1: tip.set(str_saveok) if n==2: tip.set(str_saveerror) if n==3: tip.set(str_wordempty) if n==4: tip.set(str_repeat) #主程序从这里开始 fname = 'DictForGeeksdata' data={} if fname in os.listdir('.'): for line in open(fname,'r').readlines(): line=line.strip().decode('utf-8') if '~' in line: key=line.split('~')[0] value=line.split('~')[1] data[key]=value if '~' not in line: value=value+'\n'+line data[key]=value #从这里开始写UI root=Tk() root.title('DictForGeeks') root.geometry('400x400') #查询标签 lb1=Label(root,text='查询:') lb1.place(x=100,y=20) #状态标签 tip=StringVar() lb2=Label(root,fg="blue",textvariable=tip) lb2.place(x=40,y=350) #参考列表框 lbox = Listbox(root,height=3) lbox.place(x=140,y=40) lbox.bind('<Double-Button-1>',printList) #输出显示窗口 t1=Text(root,height=10,width=30) t1.bind("<Key>",TextChange) t1.place(x=80,y=100) #查询窗口 e=StringVar() e.trace("w", lambda name, index, mode: callback(e)) entry=Entry(root,textvariable=e) entry.place(x=140,y=20) entry.focus_set() #按钮 btn1=Button(root,text='保存',command=Save,bg='PeachPuff') btn1.place(x=340,y=160) #这里声明常用的字符串 str_cannotfind="没有找到该单词!" str_saveok="保存成功!" str_saveerror="保存失败,请修改解释!" str_wordempty="保存失败,单词或解释为空!" str_repeat="保存失败,重复保存!" str_null="" #主循环 root.mainloop()