您的位置:首页 > 编程语言 > Python开发

python项目之 英汉词典 带GUI tkinter

2016-01-10 16:51 761 查看

python项目之 英汉词典 带GUI tkinter

思路

从文本中读取全部单词,然后和输入的词对比,有的话就输出中文的释义。

知识点

文本读取

类和对象的使用

GUI界面的搭建,使用tkinter

成品如下



词库的链接

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

源码如下

[code]# -*- 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()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: