本人使用的是Python3.6(32bit),在win10上運(yùn)行的

代碼如下:
from tkinter import *
import urllib.request
import urllib.parse
import json
#實(shí)現(xiàn)翻譯功能的函數(shù)
def translate(content):
url='http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=dict2.index'
#第一種方法
#head必須是字典類型的參數(shù)
head={}
head['User-Agent']='Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883'
data={}
data['type']='AUTO'
data['i']= content
data['doctype']= 'json'
data['xmlVersion']= '1.8'
data['keyfrom']= 'fanyi.web'
data['ue']= 'UTF-8'
data['action']= 'FY_BY_CLICKBUTTON'
data['typoResult']='true'
data=urllib.parse.urlencode(data).encode('utf-8')
req=urllib.request.Request(url,data,head)
response=urllib.request.urlopen(req)
html=response.read().decode('utf-8')
target=json.loads(html)
result = target['translateResult'][0][0]['tgt']
return result
#實(shí)現(xiàn)界面功能
root =Tk()
root.title('中英互譯')
frame = Frame(root)
frame.grid(padx = 10, pady = 10)
v2 = StringVar()
Label(frame,text='輸入要翻譯的文字:').grid(row=0,column=0)
Label(frame,text='翻譯之后的結(jié)果:').grid(row=5,column=0)
e1 = Entry(frame)
e1.grid(row = 0, column = 1)
e2 = Entry(frame, textvariable=v2, state='readonly')\
.grid(row=5, column=1)
def trans():
r = translate(e1.get())
v2.set(r)
#print(r)
Button(root,text='翻譯',width=10,command=trans)\
.grid(row=10,column=0,sticky=W)
Button(root,text='退出',width=10,command=root.quit)\
.grid(row=10,column=1,sticky=E)
mainloop()
分享名稱:Python3.6實(shí)現(xiàn)帶有簡單界面的有道翻譯小程序-創(chuàng)新互聯(lián)
URL分享:http://www.chinadenli.net/article40/dposeo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、網(wǎng)站制作、建站公司、做網(wǎng)站、App設(shè)計(jì)、網(wǎng)站設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容