# -*-coding:utf-8-*-
from tkinter import *
import requests
def translate(event=None):
content = et.get()
url = r'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'}
data = {}
data['i'] = content
data['type'] = 'AUTO'
data['doctype'] = 'json'
target = requests.post(url, data=data, headers=headers).json()
target = target['translateResult'][0][0]['tgt']
var.set(target)
return target
root = Tk()
root.title("翻译")
root.geometry("297x78")
root.resizable(0, 0)
# 翻译结果
var = StringVar()
Label(root, text="翻译内容:").grid(row=0, column=0, sticky=W)
et = Entry(root, width=30)
et.grid(row=0, column=1, sticky=W)
et.bind("<Key-Return>", translate)
et.focus()
lb = Label(root, text="翻译结果:")
lb.grid(row=1, column=0, sticky=W)
et2 = Entry(root, state="readonly", width=30, textvariable=var)
et2.grid(row=1, column=1, sticky=W)
Button(root, text="翻译", width=10, command=translate).grid(row=2, column=0)
Button(root, text="退出", width=10, command=lambda x=0 : root.destroy()).grid(row=2, column=1, sticky=E)
mainloop()