简单翻译是一款简单好用的翻译工具,轻松开始翻译,支持英语、日语、韩语等的互译,调用有道词典在线翻译,不用安装,解压即可使用!
使用说明
代码:
import urllib.request
import urllib.parse
import json
import tkinter
import tkinter
root = tkinter.Tk()
root.title(
"简单翻译 v2.0")
root.geometry(
'325x300')
width =
325
height =
325
screenwidth =root.winfo_screenwidth()
screenheight = root.winfo_screenheight()
alignstr =
'%dx%d+%d+%d' % (width,height, (screenwidth-width)/
2,
(screenheight-height)/
2)
root.geometry(alignstr)
def hit_me():
content = t1.get(
"0.0",
"end")
temp_content = content.replace(
'。',
'.',
content.count(
'。'))
#把句号换成点
# 从Request URL:拷贝过来。把_o删了
url =
"http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule"
# data就是表单数据,把Form Data 中的内容拷贝过来
data = {}
data[
'i'] = temp_content
data[
'from'] =
'AUTO'
data[
'to'] =
'AUTO'
data[
'smartresult'] =
'dict'
data[
'client'] =
'fanyideskweb'
data[
'salt'] =
'15803439446390'
data[
'sign'] =
'8e349204c5d1140741ffe43284595085'
data[
'ts'] =
'1580343944639'
data[
'bv'] =
'bbb3ed55971873051bc2ff740579bb49'
data[
'doctype'] =
'json'
data[
'version'] =
'2.1'
data[
'keyfrom'] =
'fanyi.web'
data[
'action'] =
'FY_BY_CLICKBUTTION'
# 使用urllib.parse.urlencode()函数将字符串转换为所需要的形式
# 把Unicode的文件格式转换为uf-8的编码形式
data = urllib.parse.urlencode(data).encode(
'utf-8')
response = urllib.request.urlopen(url, data)
# 解码的时候也要用uf-8来解码
html = response.read().decode(
"utf-8")
target = json.loads(html)
t2.delete(
"0.0",
"end")
t2_text=(target[
"translateResult"][
0][
0][
"tgt"])
t2.insert(
1.0,t2_text)
# 第5步,在窗口界面设置放置Button按键
# 在图形界面上设定输入框控件entry并放置控件
t1 = tkinter.Text(root,
show=
None,
font=(
'Arial',
14))
t2 = tkinter.Text(root,
show=
None,
font=(
'Arial',
14))
l1=tkinter.Label(root,
text=
' 调用有道词典在线翻译. by:张嘉',
font=(
'Arial',
12),
#font字体
width=
20,
height=
2)
# t1.Text(root, height=3,wrap=WORD)
b1 = tkinter.Button(root,
text=
'翻译一下',
font=(
'Arial',
12),
width=
10,
height=
1,
command=hit_me)
t1.place(
x=
10,
y=
10,
width=
300,
height=
100)
b1.place(
x=
10,
y=
120)
t2.place(
x=
10,
y=
165,
width=
300,
height=
100)
l1.place(
x=
10,
y=
290,
width=
300,
height=
20)
root.mainloop()