python用tkinter實現(xiàn)一個gui的翻譯工具
更新時間:2020年10月26日 10:49:55 作者:凹凸曼大人
這篇文章主要介紹了python用tkinter實現(xiàn)一個gui的翻譯工具,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
+
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from tkinter import *
import hashlib
import time
import json
import requests
import random
LOG_LINE_NUM = 0
class MY_GUI():
def __init__(self,init_window_name):
self.init_window_name = init_window_name
self.headers = {
'User-Agent': '自己的User-Agent',
'Referer': 'http://fanyi.youdao.com/',
'Cookie': '自己的Cookie'
}
self.data = {
'i': None,
'from': 'AUTO',
'to': 'AUTO',
'smartresult': 'dict',
'client': 'fanyideskweb',
'salt': None,
'sign': None,
'ts': None,
'bv': None,
'doctype': 'json',
'version': '2.1',
'keyfrom': 'fanyi.web',
'action': 'FY_BY_REALTlME'
}
self.url = 'http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule'
#設(shè)置窗口
def set_init_window(self):
self.init_window_name.title("翻譯工具_v1.0") #窗口名
#self.init_window_name.geometry('320x160+10+10') #290 160為窗口大小,+10 +10 定義窗口彈出時的默認展示位置
self.init_window_name.geometry('1068x681+10+10')
#self.init_window_name["bg"] = "pink" #窗口背景色,其他背景色見:blog.csdn.net/chl0000/article/details/7657887
#self.init_window_name.attributes("-alpha",0.9) #虛化,值越小虛化程度越高
#標簽
self.init_data_label = Label(self.init_window_name, text="待處理數(shù)據(jù)")
self.init_data_label.grid(row=0, column=0)
self.result_data_label = Label(self.init_window_name, text="輸出結(jié)果")
self.result_data_label.grid(row=0, column=12)
self.log_label = Label(self.init_window_name, text="日志")
self.log_label.grid(row=12, column=0)
#文本框
self.init_data_Text = Text(self.init_window_name, width=67, height=35) #原始數(shù)據(jù)錄入框
self.init_data_Text.grid(row=1, column=0, rowspan=10, columnspan=10)
self.result_data_Text = Text(self.init_window_name, width=70, height=49) #處理結(jié)果展示
self.result_data_Text.grid(row=1, column=12, rowspan=15, columnspan=10)
self.log_data_Text = Text(self.init_window_name, width=66, height=9) # 日志框
self.log_data_Text.grid(row=13, column=0, columnspan=10)
#按鈕
self.str_trans_to_md5_button = Button(self.init_window_name, text="轉(zhuǎn)換", bg="lightblue", width=10,command=self.str_trans) # 調(diào)用內(nèi)部方法 加()為直接調(diào)用
self.str_trans_to_md5_button.grid(row=1, column=11)
#功能函數(shù)
def str_trans(self):
word = self.init_data_Text.get(1.0,END).strip().replace("\n","")
#print("src =",word)
if word:
try:
ts = str(int(time.time() * 10000))
salt = str(int(time.time() * 10000) + random.random() * 10 + 10)
sign = 'fanyideskweb' + word + salt + ']BjuETDhU)zqSxf-=B#7m'
sign = hashlib.md5(sign.encode('utf-8')).hexdigest()
bv = '5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36'
bv = hashlib.md5(bv.encode('utf-8')).hexdigest()
self.data['i'] = word
self.data['salt'] = salt
self.data['sign'] = sign
self.data['ts'] = ts
self.data['bv'] = bv
re = requests.post(self.url, headers=self.headers, data=self.data)
jieguo = re.json()['translateResult'][0][0].get('tgt')
#print(jieguo)
#輸出到界面
self.result_data_Text.delete(1.0,END)
self.result_data_Text.insert(1.0,jieguo)
self.write_log_to_Text("INFO:翻譯 success")
except:
self.result_data_Text.delete(1.0,END)
self.result_data_Text.insert(1.0,"翻譯失敗")
else:
self.write_log_to_Text("ERROR:str_trans failed")
#獲取當前時間
def get_current_time(self):
current_time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
return current_time
#日志動態(tài)打印
def write_log_to_Text(self,logmsg):
global LOG_LINE_NUM
current_time = self.get_current_time()
logmsg_in = str(current_time) +" " + str(logmsg) + "\n" #換行
if LOG_LINE_NUM <= 7:
self.log_data_Text.insert(END, logmsg_in)
LOG_LINE_NUM = LOG_LINE_NUM + 1
else:
self.log_data_Text.delete(1.0,2.0)
self.log_data_Text.insert(END, logmsg_in)
def gui_start():
init_window = Tk() #實例化出一個父窗口
ZMJ_PORTAL = MY_GUI(init_window)
# 設(shè)置根窗口默認屬性
ZMJ_PORTAL.set_init_window()
init_window.mainloop() #父窗口進入事件循環(huán),可以理解為保持窗口運行,否則界面不展示
gui_start()
運行效果:

自己可以用pyinstaller 打包成 exe隨時可以用。

省去了再打開網(wǎng)頁去搜 索翻譯網(wǎng)頁,下載翻譯軟件。
以上就是python用tkinter實現(xiàn)一個gui的翻譯工具的詳細內(nèi)容,更多關(guān)于python 翻譯工具的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python和OpenCV進行指紋識別與驗證的實現(xiàn)
本文主要介紹了Python和OpenCV進行指紋識別與驗證的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2025-03-03
Python中使用select模塊實現(xiàn)非阻塞的IO
這篇文章主要介紹了Python中使用select模塊實現(xiàn)非阻塞的IO,本文使用一個簡單聊天室程序講解Python中的select模塊使用,需要的朋友可以參考下2015-02-02
pytorch 實現(xiàn)將自己的圖片數(shù)據(jù)處理成可以訓練的圖片類型
今天小編就為大家分享一篇pytorch 實現(xiàn)將自己的圖片數(shù)據(jù)處理成可以訓練的圖片類型,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01
使用Python實現(xiàn)BT種子和磁力鏈接的相互轉(zhuǎn)換
這篇文章主要介紹了使用Python實現(xiàn)BT種子和磁力鏈接的相互轉(zhuǎn)換的方法,有時比如迅雷無法加載磁力鏈接或者無法上傳附件分享時可以用到,需要的朋友可以參考下2015-11-11
Pygame實戰(zhàn)之實現(xiàn)經(jīng)典外星人游戲
這篇文章主要介紹了通過Pygame實現(xiàn)經(jīng)典的外星人游戲的示例代碼,文中的代碼講解詳細,對我們了解Pygame有一定的幫助,感興趣的同學可以試一試2022-01-01

