發現馬來朋友使用的英英在線詞典比較好用,于是想做一個英英詞典的小程序。程序的實現主要包括兩部分,1是完成在網頁上有效信息的提取,2是python的界面開發。爬蟲部分代碼很簡單。但到了界面部分開發時遇到了一些小問題。為了讓英文解釋的展示看上去更漂亮一些。需要在使用tkInter的Text控件進行展示時做如下工作:
(1)傳入一個長度很長的string類型變量,不同行需要插入換行符
(2)導入Text()后需要有逐行識別該行類型的功能,該行的類型是詞典名稱、同義詞展示還是單詞解釋?這個 應該很簡單,詞典名稱用等于號來識別,同義詞固定空多個空格,這樣三種就可以識別了
(3)區分了不同的類型后,需要針對該行進行字體的調整, 這部分百度了好久,才知道答案
使用addtag(tagName,第一個字符位置,最后一個字符位置)函數來定義一個標簽名稱,而后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')#設置窗口大小和位置self.window.geometry('700x700+500+200')#self.window.minsize(700,700)#self.window.maxsize(700,700)#創建一個文本框#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)#創建一個按鈕#為按鈕添加事件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)#翻譯結果標題self.title_label = Label(self.window,text=u'結果:')self.title_label.place(x=10,y=25)#翻譯結果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把這個值傳送給服務器進行翻譯result = self.fanyi.crawl(content)#將結果顯示在窗口中的文本框中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#清空文本域中的內容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"," ")#把這個值傳送給服務器進行翻譯self.submit()self.result_text1.delete(0.0, END)#result = self.fanyi.crawl(content)#將結果顯示在窗口中的文本框中#self.result_text.delete(0.0,END)#self.result_text.insert(END,result)
if __name__=="__main__":app = Application()app.run()
總結
以上是生活随笔為你收集整理的python英英字典开发过程中学到的tkFont的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。