from tkinter import *
root =Tk()
sky=StringVar() #输入汉语
et=Entry(root,textvariable=sky,width=20)
t = Text(root,width=80) # 显示字典词条
t.grid(row=0,column=0,columnspan=2)
#-------------------
sf=open("ced.txt","r",encoding="utf-8") # 打开源词典,我这里修改了cedict 的名字
sflist=sf.readlines()
sf.close()
dictce={}
#for i in range(len(sflist)):
for i in range(len(sflist)): #制作内部词典
x=sflist[i]
p=x.find("[")
q=x.find("/")
m=x[:p].split(" ")[1]
dictce[m]=x[q+1:]
#------------------------------------
def show_mean_list(rt):
rt=rt.replace("/","\n")
return(rt)
def seek_meaning(ev):
t.delete(1.0,END)
seekword=sky.get().strip()
if seekword=="": return
v=dictce.keys()
w=[i for i in v if seekword in i]
if w==[]: t.insert(INSERT,"----none----");return
ct=0
for j in w:
t.tag_config('a',foreground = 'green')
t.tag_config('b',foreground = 'blue')
t.insert(INSERT, str(ct)+"--> "+j+"\n",'a')
rt=dictce.get(j)
t.insert(INSERT, show_mean_list(rt),"b")
ct+=1
et.bind("<Return>",seek_meaning) # 输入汉语,并回车
t.insert(1.0," \n Enjoy yourself , My dear friend ! \n \n -- by 健忘的牙膏")
l=Label(root, text="查询:",width=10)
l.grid(row=1,column=0,sticky =E) # 以上3行可以cut掉
et.grid(row=1,column=1,sticky =W)
root.mainloop()