發(fā)現(xiàn)馬來朋友使用的英英在線詞典比較好用,于是想做一個(gè)英英詞典的小程序。程序的實(shí)現(xiàn)主要包括兩部分,1是完成在網(wǎng)頁上有效信息的提取,2是python的界面開發(fā)。爬蟲部分代碼很簡單。但到了界面部分開發(fā)時(shí)遇到了一些小問題。為了讓英文解釋的展示看上去更漂亮一些。需要在使用tkInter的Text控件進(jìn)行展示時(shí)做如下工作:
 (1)傳入一個(gè)長度很長的string類型變量,不同行需要插入換行符
 (2)導(dǎo)入Text()后需要有逐行識(shí)別該行類型的功能,該行的類型是詞典名稱、同義詞展示還是單詞解釋?這個(gè) 應(yīng)該很簡單,詞典名稱用等于號(hào)來識(shí)別,同義詞固定空多個(gè)空格,這樣三種就可以識(shí)別了
 (3)區(qū)分了不同的類型后,需要針對(duì)該行進(jìn)行字體的調(diào)整, 這部分百度了好久,才知道答案
 使用addtag(tagName,第一個(gè)字符位置,最后一個(gè)字符位置)函數(shù)來定義一個(gè)標(biāo)簽名稱,而后tag_config(tgName, foreground=‘green4’,font=tkFont.Font(family=‘Courier New Baltic’, size=12, weight=tkFont.NORMAL))來定義具體的字體和字體顏色,字體的所有顏色的引用方法在此鏈接中查看http://www.science.smith.edu/dftwiki/index.php/Color_Charts_for_TKinter
 
效果如下:
 
 具體代碼如下:
 
import requests,bs4
import urllib
from tkinter import *
import tkinter.font as tkFont
class YouDaoFanyi(object):def __init__(self):passdef crawl(self,word):url = 'https://www.definitions.net/definition/' + str(word)headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36'}res = requests.get(url,headers=headers)body=bs4.BeautifulSoup(res.text,'html.parser')dicts=body.find(id='definitions-list').find_all('div',class_='rc5')dictText=''for i in dicts:dictName=i.find('h3').find('span').textif dictName=='Princeton\'s WordNet':dictText = dictText+dictName+'\n'li=i.find('ol').find_all('li')for j in li:plist=j.find_all('p')if len(plist)==3:dictText = dictText + '\n'dictText=dictText+plist[0].text+'\n'dictText = dictText +'  '+plist[1].text+'\n'dictText = dictText +'    '+plist[2].text+'\n'elif len(plist)==2:dictText = dictText + '\n'dictText = dictText +plist[0].text+'\n'dictText = dictText +'  '+plist[1].text+'\n'elif dictName=='Wiktionary':dictText = dictText+dictName+'\n'li=i.find('ol').find_all('li')for j in li:plist=j.find_all('p')if len(plist)==3:dictText = dictText + '\n'dictText=dictText+plist[0].text+'\n'dictText = dictText +'  '+plist[1].text+'\n'dictText = dictText +'    '+plist[2].text+'\n'elif len(plist)==2:dictText = dictText + '\n'dictText = dictText +plist[0].text+'\n'dictText = dictText +'  '+plist[1].text+'\n'elif dictName == 'Webster Dictionary':dictText = dictText + '\n'dictText = dictText + '\n'dictText = dictText +dictName+'\n'li = i.find('ol').find_all('li')for j in li:plist = j.find_all('p')dictText = dictText + '\n'dictText = dictText + plist[0].text+'\n'dictText = dictText +'  '+plist[1].text+'\n'return dictTextclass Application(object):def __init__(self):self.window = Tk()self.fanyi = YouDaoFanyi()#print(tkFont.families())#打印可選字體self.window.title(u'詞典 designed by zhengyb')#設(shè)置窗口大小和位置self.window.geometry('700x700+500+200')#self.window.minsize(700,700)#self.window.maxsize(700,700)#創(chuàng)建一個(gè)文本框#self.entry = Entry(self.window)#self.entry.place(x=10,y=10,width=200,height=25)#self.entry.bind("<Key-Return>",self.submit1)self.result_text1 = Text(self.window,background = 'azure',font='bold')# 喜歡什么背景色就在這里面找哦,但是有色差,得多試試:http://www.science.smith.edu/dftwiki/index.php/Color_Charts_for_TKinterself.result_text1.place(x = 10,y = 5,width = 680,height = 20)self.result_text1.bind("<Key-Return>",self.submit1)#創(chuàng)建一個(gè)按鈕#為按鈕添加事件self.submit_btn = Button(self.window,text=u'查詢',command=self.submit)self.submit_btn.place(x=255,y=25,width=35,height=25)self.submit_btn2 = Button(self.window,text=u'清空',command = self.clean)self.submit_btn2.place(x=300,y=25,width=35,height=25)#翻譯結(jié)果標(biāo)題self.title_label = Label(self.window,text=u'結(jié)果:')self.title_label.place(x=10,y=25)#翻譯結(jié)果self.result_text = Text(self.window,background = 'snow2')self.result_text.place(x = 10,y = 50,width = 680,height = 600)#回車翻譯def submit(self):#從輸入框獲取用戶輸入的值content = self.result_text1.get(0.0,END).strip().replace("\n"," ")#print把這個(gè)值傳送給服務(wù)器進(jìn)行翻譯result = self.fanyi.crawl(content)#將結(jié)果顯示在窗口中的文本框中self.result_text.delete(0.0,END)self.result_text.insert(END, result)#self.result_text.insert(INSERT, 'I love study very well')rowlist=result.split('\n')count=1rowNo=1strIcld=content+'('str2Icld=content.title()+'('for m in rowlist:if (strIcld in m) or (str2Icld in m) or ((',' in m) and('(' in m) and content in m):st=str(count)+'.0'ed=str(count)+'.'ed=ed+str(m.find('('))st2 = str(count) + '.'st2=st2+str(m.find('(')+1)ed2 = str(count) + '.200'tgName='tag'+str(rowNo)rowNo=rowNo+1tgName1 = 'tag' + str(rowNo)rowNo=rowNo+1
#				print(st+' '+ed+' '+tgName)self.result_text.tag_add(tgName, st,ed)self.result_text.tag_config(tgName, foreground='black',font=tkFont.Font(family='Arial', size=14, weight=tkFont.BOLD))self.result_text.tag_add(tgName1, st2, ed2)self.result_text.tag_config(tgName1, foreground='gray',font=tkFont.Font(family='Helvetica', size=12, weight=tkFont.NORMAL))elif '    ' in m:st4 = str(count) + '.0'ed4 = str(count) + '.200'tgName3 = 'tag' + str(rowNo)rowNo = rowNo + 1self.result_text.tag_add(tgName3, st4, ed4)self.result_text.tag_config(tgName3, foreground='green4',font=tkFont.Font(family='Courier New Baltic', size=12, weight=tkFont.NORMAL))elif m=='Wiktionary' or m=='Webster Dictionary' or m=='Princeton\'s WordNet':st5 = str(count) + '.0'ed5 = str(count) + '.200'tgName4 = 'tag' + str(rowNo)rowNo = rowNo + 1self.result_text.tag_add(tgName4, st5, ed5)self.result_text.tag_config(tgName4, foreground='dark blue',font=tkFont.Font(family='Courier New Baltic', size=15, weight=tkFont.BOLD))else:st3 = str(count) + '.0'ed3=str(count)+'.200'tgName2 = 'tag' + str(rowNo)rowNo=rowNo+1self.result_text.tag_add(tgName2, st3, ed3)self.result_text.tag_config(tgName2, foreground='violet red',font=tkFont.Font(family='Arial', size=12, weight=tkFont.NORMAL))count=count+1#清空文本域中的內(nèi)容def clean(self):self.result_text1.delete(0.0,END)self.result_text.delete(0.0,END)def run(self):self.window.mainloop()def submit1(self,event):#從輸入框獲取用戶輸入的值#content = self.result_text1.get(0.0,END).strip().replace("\n"," ")#把這個(gè)值傳送給服務(wù)器進(jìn)行翻譯self.submit()self.result_text1.delete(0.0, END)#result = self.fanyi.crawl(content)#將結(jié)果顯示在窗口中的文本框中#self.result_text.delete(0.0,END)#self.result_text.insert(END,result)
if __name__=="__main__":app = Application()app.run()
                            
總結(jié)
                            
                                以上是生活随笔為你收集整理的python英英字典开发过程中学到的tkFont的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
                            
                            
                                如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。