python 编写一个中英文翻译器

import urllib.request
import urllib.parse
import json
from tkinter import *
root=Tk()
root.title("翻译小程序")
sw = root.winfo_screenwidth()
#得到屏幕宽度
sh = root.winfo_screenheight()
#得到屏幕高度
ww = 500
wh = 300
x = (sw-ww) / 2
y = (sh-wh) / 2-50
root.geometry("%dx%d+%d+%d" %(ww,wh,x,y))
lb2=Label(root,text="输入英文翻译中文,或者输入中文翻译英文,按回车键翻译。--版权所有,翻录必究。")
lb2.place(relx=0, rely=0.05)
txt = Text(root,font=("宋体",20))
txt.place(rely=0.6, relheight=0.4,relwidth=1)
inp1 = Entry(root,font=("",20))
inp1.place(relx=0, rely=0.2, relwidth=1, relheight=0.25)
def run2(event):
    txt.delete("0.0",END)
    a = (inp1.get())
    content = a
    url='http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
    data={}
    data['i'] = content
    data['from'] = 'AUTO'
    data['to'] = 'AUTO'
    data['smartresult'] = 'dict'
    data['client'] = 'fanyideskweb'
    data['salt'] = '15812376682056'
    data['sign'] = 'a1246b257926af8432be022564ff79f5'
    data['ts'] = '1581237668205'
    data['bv'] = '656f750600466990f874a839d9f5ad23'
    data['doctype'] = 'json'
    data['version'] = '2.1'
    data['keyfrom'] = 'fanyi.web'
    data['action'] = 'FY_BY_CLICKBUTTION'
    data = urllib.parse.urlencode(data).encode('utf-8')
    response = urllib.request.urlopen(url,data)
    html = response.read().decode('utf-8')
    target = json.loads(html)
    s=("%s"%(target['translateResult'][0][0]['tgt'])+"\n")
    print(s)
    txt.insert(END, s)
def button1(event):
    btn1 = Button(root, text='结果如下', font=("",12))
    btn1.place(relx=0.35, rely=0.45, relwidth=0.2, relheight=0.15)
inp1.bind("",run2)
button1(1)
root.mainloop()

python 编写一个中英文翻译器_第1张图片

你可能感兴趣的:(python 编写一个中英文翻译器)