DictForGeeks,我本身寫的自定義詞庫的詞典,支持英-中,中-英,也能夠當電話本用。

DictForGeeks是我的寫的一款詞典軟件,支持高度自定義。我的在學習過程當中,發現不少科技詞彙的意思跟主流的詞義很不同就想寫一款能夠本身定義詞義的詞典,支持添加和查詢功能。至少要比txt記單詞要方便。因而便有了這麼個小程序。語言使用python,GUI使用的是tkinter,原本想用wxpython的,可是考慮到不少pythoner沒有安裝wxpython,仍是決定用它自帶的GUI了。python

用法:第一個窗口是單詞輸入窗口;第二個是提示窗口,支持雙擊選擇;第三個窗口是解釋窗口,能夠隨時編輯;旁邊有個「保存」按鈕,能夠把修改好的保存起來;最下面還有一個提示欄,提示是否保存成功。小程序

 

# -*- 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: jiangkun.here@gmail.com
"""

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()


相關文章
相關標籤/搜索