python項目之 英漢詞典 帶GUI tkinter

python項目之 英漢詞典 帶GUI tkinter

思路

從文本中讀取全部單詞,然後和輸入的詞對比,有的話就輸出中文的釋義。

知識點

文本讀取
類和對象的使用
GUI界面的搭建,使用tkinter

成品如下

英漢詞典

詞庫的鏈接

http://pan.baidu.com/s/1jHw2n0y

源碼如下

# -*- coding: utf-8 -*-
###################################
##coding by 劉雲飛
###################################
import os
import sqlite3
from tkinter import *

class One_Word(object):
    def __init__(self):
        self.en = u""
        self.num = 0
        self.chs = []
    def set_word(self, en, num, chs):
        self.num = num
        self.chs = chs
        self.en = en

def read_file():
    words =[]
    with open('wwaa.txt','r',encoding='utf8') as f:
        while True:
            line = f.readline().strip('\n')
            if line == "":
                break
            wod = line.split("=")
            en = wod[1]
            nums = f.readline().strip('\n').split("=")
            num = int(nums[1])
            i=0
            chs = []
            while i< num:
                f.readline()
                chs.append(f.readline().strip('\n'))
                i += 1
            word= One_Word()
            word.en = en
            word.chs = chs
            word.num = num
            words.append(word)
    return words

def cha_xun(danci,words):
    flag = False
    chs = ""
    for word in words:
        if flag == True:
            break
        if danci == str(word.en):
            num = word.num
            chss = word.chs
            flag =True
            for chsa in chss:
                chs += chsa
                chs +="\n"
    return chs

def get_word():
    w_en = ent_cha.get()
    #print(w_en)
    chs = cha_xun(w_en,words)
    if chs == "":
        strs = "Not find word ->"+ w_en
        text_chs.set(strs)
    else:
        text_chs.set(chs)

words = read_file()

gui = Tk()
gui.title('英漢詞典 by 劉雲飛')
gui.geometry('600x480')
lab_cha = Label(gui,text = "查詢:",font = 'Times -20',\
                width = 8,height = 2)
ent_cha = Entry(gui,font = 'Times -20',width = 40)
lab_shiyi = Label(gui,text = "釋義:",font = 'Times -20',\
                  width = 8,height = 2)
text_chs = StringVar()
ent_shiyi = Label(gui,textvariable =text_chs,\
                  font = '宋體 -20',bg = 'white',\
                  width = 40,height = 17)
lab_cha.grid(row = 0,column = 0)
ent_cha.grid(row = 0,column = 1)
lab_shiyi.grid(row = 1,column = 0)
ent_shiyi.grid(row = 1,column = 1)
labss = Label(gui,text = "",font = 'Times -20',\
                width = 1,height = 1)
labss.grid(row = 2,column = 0)
btn_cha = Button(gui,text = "查詢",command = get_word,\
                 font = 'Times -20',width = 10,height = 1)
btn_cha.grid(row = 3,column = 1)
gui.mainloop()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章