簡單翻譯軟件是一個(gè)可以自助翻譯的小工具,可以讀入多種語種如英語、日語、韓語等的翻譯服務(wù)。python寫的,搗鼓了一個(gè)界面,輸入英文單詞,回車,如果詞庫存在該單詞,輸出對應(yīng)的中文意思,不存在則提示不存在。
軟件特色:
1、可以翻譯菜單,編輯框等控件;
2、支持中文版本;
3、軟件完全免費(fèi)無毒!
代碼說明:
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('。')) #把句號換成點(diǎn)
# 從Request URL:拷貝過來。把_o刪了
url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule"
# data就是表單數(shù)據(jù),把Form Data 中的內(nèi)容拷貝過來
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()函數(shù)將字符串轉(zhuǎn)換為所需要的形式
# 把Unicode的文件格式轉(zhuǎn)換為uf-8的編碼形式
data = urllib.parse.urlencode(data).encode('utf-8')
response = urllib.request.urlopen(url, data)
# 解碼的時(shí)候也要用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步,在窗口界面設(shè)置放置Button按鍵
# 在圖形界面上設(shè)定輸入框控件entry并放置控件
t1 = tkinter.Text(root, show=None, font=('Arial', 14))
t2 = tkinter.Text(root, show=None, font=('Arial', 14))
l1=tkinter.Label(root, text=' 調(diào)用有道詞典在線翻譯. 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()