代码
from tkinter import *
import requests
# 翻译
def translate():
content = entry.get()
url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
headers = {
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Host': 'fanyi.youdao.com',
'Origin': 'http://fanyi.youdao.com',
'Referer': 'http://fanyi.youdao.com/',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.106 Safari/537.36'
}
data = {
'i': content,
'from': 'AUTO',
'to': 'AUTO',
'smartresult': 'dict',
'client': 'fanyideskweb',
'salt': '15821220371529',
'sign': 'fb1d1fd8b09781675b8db268b6e646bd',
'ts': '1582122037152',
'bv': '79c3b9bb42d51a304cb661e9dab01b7d',
'doctype': 'json',
'version': '2.1',
'keyfrom': 'fanyi.web',
'action': 'FY_BY_CLICKBUTTION'
}
response = requests.post(url, headers=headers, data=data)
json_str = response.json()
txt = content + ':' + json_str['translateResult'][0][0]['tgt']
text.insert(END, txt)
text.see(END)
text.update()
# 窗口
root = Tk()
root.title('翻译')
root.geometry('560x450+500+200')
# 文字
lable = Label(root, text='请输入文本:', font=('楷体', 20))
lable.grid()
# 输入框
entry = Entry(root, font=('楷体', 20))
entry.grid(row=0, column=1)
# 按钮
button1 = Button(root, text = '开始翻译', font=('楷体', 15), command=translate)
button2 = Button(root, text = '退出程序', font=('楷体', 15), command=root.quit)
button1.grid(row=1, column=0, sticky=W)
button2.grid(row=1, column=1, sticky=E)
# 提示文字
text = Listbox(root, font=('楷体', 16), width=50, height=15)
text.grid(row=2, columnspan=2)
root.mainloop()
tkinter完成的翻译小程序