好友
阅读权限20
听众
最后登录1970-1-1
|
python写的,捣鼓了一个界面,1.0版本不能识别句号。2.0解决了这个问题
成品:链接:https://pan.baidu.com/s/1SpwEY3vnGj_iJ_SGexQo_w 提取码:kyqj
代码:
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() |
免费评分
-
查看全部评分
|
发帖前要善用【论坛搜索】功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。 |
|
|
|
|