dicproject

来源:互联网 发布:网络运营可以自学吗 编辑:程序博客网 时间:2024/06/06 12:47

-- coding: utf-8 --

“””
Created on Fri Aug 19 11:42:07 2016

@author: 钟民
“”“

import tkinter as tk
from bs4 import BeautifulSoup as bs
import matplotlib.pyplot as plt
import requests
import re
def checkword(word):
form={‘q’:word}
rs=requests.get(‘http://cn.bing.com/dict/‘,params=form)
soup=bs(rs.content,’lxml’)
content=soup.findAll(‘span’,{‘class’:’def’})
explain={}
for i in content:
data=i.get_text()
explain[word]=data
fr=open(‘D:\word.txt’,’a’)
fr.write(‘\n’+str(explain))
fr.close
return str(explain)
def command():
word=entry1.get()
text=checkword(word)
t.tag_config(‘tag_1’,foreground=’red’)
t.tag_config(‘tag_2’,foreground=’blue’)
count=WordCount()
if count.get(word) >1 and count.get(word)<=2:
t.insert(0.0,’\n’+text,’tag_1’)
if count.get(word) ==1:
t.insert(0.0,’\n’+text)
if count.get(word)>2:
t.insert(0.0,’\n’+text,’tag_2’)
entry1.delete(0,tk.END)
t.pack()
def WordCount():
fr=open(‘D:\word.txt’)
arrayOflines=fr.readlines()
fr.close()
dic={}
wordlist=[]
for line in arrayOflines:
word=re.findall(”’{‘(.*?)’:”’,line)
explain=re.findall(”’: ‘(.*?)’}”’,line)
if len(word) !=0:
wordlist.append(word)
dic[word[0]]=dic.get(word[0],0)+1
return dic
window=tk.Tk()
window.wm_attributes(‘-topmost’,1)#显示最前面
window.geometry(‘200x220+1150+100’)
window.title(‘my dictionary’)
entry1=tk.Entry(window)
entry1.pack()
button1=tk.Button(window,text=u’翻译’,command=command)
button1.pack()
t=tk.Text()
t.pack()
window.mainloop()

0 0
原创粉丝点击