'''
@Time : 2021/2/6 15:55
@Author : WGS
@remarks :
'''
import urllib.request
import urllib.parse
import json
from tkinter import *
root = Tk()
root.title("WGS Translate")
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 run1():
run2('event')
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), command=run1)
btn1.place(relx=0.35, rely=0.45, relwidth=0.2, relheight=0.15)
inp1.bind("" , run2)
button1(1)
root.mainloop()
为了方便使用,将python程序打包成可执行文件,不再依赖编程环境
下载pyinstaller
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyinstaller
在cmd命令中执行
pyinstaller -F XXX.py
py3打包成exe:
https://blog.csdn.net/qq_42363032/article/details/112280907