import tkinter as tk |
from tkinter import ttk |
from tkinter import scrolledtext |
from tkinter import Menu |
from tkinter import messagebox |
from tkinter import filedialog |
from PIL import Image, ImageTk |
import webbrowser |
import random |
import requests |
import pygame |
import hashlib |
import easygui as gui |
from aip import AipSpeech |
import os |
|
root = tk.Tk() |
root.geometry( "550x430" ) |
root.resizable( False , False ) |
root.title( "翻译器" ) |
# im = Image.open("a.ico") |
# img = ImageTk.PhotoImage(im) |
# root.tk.call('wm', 'iconphoto', root._w, img) |
canvas = tk.Canvas(root,bg = 'lightgrey' ,height = 430 ,width = 550 ) #绘制画布 |
canvas.pack() |
|
i = 1 |
class ToolTip( object ): |
def __init__( self , widget): |
self .widget = widget |
self .tipwindow = None |
self . id = None |
self .x = self .y = 0 |
def showtip( self , text): |
"Display text in tooltip window" |
self .text = text |
if self .tipwindow or not self .text: |
return |
x, y, _cx, cy = self .widget.bbox( "insert" ) |
x = x + self .widget.winfo_rootx() + 27 |
y = y + cy + self .widget.winfo_rooty() + 27 |
self .tipwindow = tw = tk.Toplevel( self .widget) |
tw.wm_overrideredirect( 1 ) |
tw.wm_geometry( "+%d+%d" % (x, y)) |
|
label = tk.Label(tw, text = self .text, justify = tk.LEFT, |
background = "#ffffe0" , relief = tk.SOLID, borderwidth = 1 , |
font = ( "tahoma" , "8" , "normal" )) |
label.pack(ipadx = 1 ) |
def hidetip( self ): |
tw = self .tipwindow |
self .tipwindow = None |
if tw: |
tw.destroy() |
|
def createToolTip( widget, text): |
toolTip = ToolTip(widget) |
def enter(event): |
toolTip.showtip(text) |
def leave(event): |
toolTip.hidetip() |
widget.bind( '<Enter>' , enter) |
widget.bind( '<Leave>' , leave) |
|
def _quit(): |
global root |
root.quit() |
root.destroy() |
exit() |
|
def take_help_phono(): |
messagebox.showinfo( "提示" , "有需要请拨打:‘作者的手机号’" ) |
|
def take_help_http(): |
webbrowser. open ( "https://yuncode.net/48979/msg" ) |
|
def delContent(): |
Path = "C:/Users/zhubiping/Desktop/" |
file = filedialog.askopenfilename(initialdir = Path, title = 'Choose an text to del.' ,filetypes = [( "TXT" , "txt" )]) |
if len ( file ) ! = 0 : |
os.remove( file ) |
messagebox.showinfo( "提示" , "已删除" ) |
|
def saveContent(): |
global i |
SavePath = "C:/Users/zhubiping/Desktop/%d.txt" % i |
SaveContent = scr.get( 1.0 , 'end' ) |
if SaveContent = = "": |
messagebox.showerror( "错误" , "没有历史翻译内容" ) |
else : |
a = messagebox.askquestion( '提示' , '确定将所有翻译历史存放于此路径?\n' + SavePath) |
if a = = "yes" : |
with open (SavePath, "w" ,encoding = "utf-8" ) as f: |
f.write(SaveContent) |
scr.delete( 1.0 , 'end' ) |
else : |
SavePath = gui.enterbox( "请输入你要保存的文件路径:(包括文件名)" ) |
try : |
with open (SavePath, "w" ,encoding = "utf-8" ) as f: |
f.write(SaveContent) |
scr.delete( 1.0 , 'end' ) |
except : |
messagebox.showerror( "错误" , "该路径不存在" ) |
i + = 1 |
|
|
def delContent_ALL(): |
scr.delete( 1.0 , 'end' ) |
|
publicFlag = True |
appId = '20200306000393905' |
key = 'KiUofQTOXqxwyutYwx7H' |
tFrom = 'en' |
tTo = 'zh' |
fontPath = "./makeFont/simhei.ttf" |
infoList = [ "中译英" , "英译中" ] |
v3 = tk.StringVar() |
radVar = tk.IntVar() |
radVar. set ( 99 ) |
|
def changeLan(): |
global publicFlag,n1,t |
t = tk.Text(root,height = 1 ,width = 6 ,bg = "lightgrey" ) |
t.place(relx = 0.01 ,rely = 0.95 ) |
if publicFlag: |
t.insert(tk.INSERT, '英译中' ) |
publicFlag = False |
radSel = 1 |
else : |
t.insert(tk.INSERT, '中译英' ) |
publicFlag = True |
radSel = 0 |
createToolTip(t, '当前翻译状态' ) |
|
def radCall(): |
global publicFlag |
radSel = radVar.get() |
t = tk.Text(root,height = 1 ,width = 6 ,bg = "lightgrey" ) |
t.place(relx = 0.01 ,rely = 0.95 ) |
if radSel = = 0 : |
t.insert(tk.INSERT, '中译英' ) |
publicFlag = True |
else : |
t.insert(tk.INSERT, '英译中' ) |
publicFlag = False |
createToolTip(t, '当前翻译状态' ) |
|
def getResult( * args): |
global content |
content = entry.get() |
if content = = "": |
messagebox.showerror( "错误" , "您还没有输入内容哦" ) |
else : |
#生成随机数 |
salt = random.randint( 32768 , 65536 ) |
#生成签名 |
sign = appId + content + str (salt) + key |
md5 = hashlib.md5() |
md5.update(sign.encode( 'utf-8' )) |
sign = md5.hexdigest() |
url = 'http://api.fanyi.baidu.com/api/trans/vip/translate' |
global publicFlag,enShow,result_dst,scr |
if publicFlag: |
params = { 'q' :content, 'from' :tTo, 'to' :tFrom, 'appid' :appId, 'salt' :salt, 'sign' :sign} |
else : |
params = { 'q' :content, 'from' :tFrom, 'to' :tTo, 'appid' :appId, 'salt' :salt, 'sign' :sign} |
response = requests.get(url,params) |
data = response.json() |
result_dst = data[ 'trans_result' ][ 0 ][ 'dst' ] |
v3. set (result_dst) |
scr.insert(tk.INSERT, content + " --> " + result_dst + '\n\n' ) |
|
audioName = 0 |
def voice(): |
try : |
appId = '18958490' |
apiKey = 'Wx3eLpzBoNQSUP6AgqvkvKFk' |
secretKey = '2vyehX2VzlMwp6D6BU834GuNK0568XCD' |
|
client = AipSpeech(appId,apiKey,secretKey) |
result = client.synthesis(result_dst, 'zh' , 1 ,{ 'vol' : 6 , 'per' : 3 , 'spd' : 4 }) |
global audioName |
mp3name = "./" + str (audioName % 2 ) + ".mp3" |
if not isinstance (result, dict ): |
with open (mp3name, 'wb' ) as f: |
f.write(result) |
pygame.mixer.init() |
pygame.mixer.music.load(mp3name) |
pygame.mixer.music.play() |
audioName + = 1 |
except : |
messagebox.showerror( "错误" , "当前还没有翻译内容" ) |
|
tk.Label(canvas,text = "请输入您要翻译成英文的内容:" ,font = (fontPath, 14 ),bg = 'lightgrey' ).place(relx = 0.03 ,rely = 0.1 ) |
tk.Label(canvas,text = "翻译结果:" ,font = (fontPath, 15 ),bg = 'lightgrey' ).place(relx = 0.58 ,rely = 0.1 ) |
tk.Label(canvas,text = "翻译历史:" ,font = (fontPath, 15 ),bg = 'lightgrey' ).place(relx = 0.03 ,rely = 0.3 ) |
n1 = tk.Label(canvas,text = "中英语音切换" ,font = (fontPath, 16 ),bg = 'lightgrey' ).place(relx = 0.42 ,rely = 0.02 ) |
|
translate_B = tk.Button(canvas,text = " 翻译 " ,command = getResult,bg = 'orange' ,cursor = "hand2" ) |
translate_B.place(relx = 0.18 ,rely = 0.9 ) |
state_B = tk.Button(canvas,text = " 状态切换 " ,command = changeLan,bg = 'orange' ,cursor = "hand2" ) |
state_B.place(relx = 0.4 ,rely = 0.9 ) |
play_B = tk.Button(canvas,text = " 翻译内容播放 " ,command = voice,bg = 'orange' ,cursor = "hand2" ) |
play_B.place(relx = 0.68 ,rely = 0.9 ) |
entry = tk.Entry(canvas,font = (fontPath, 14 )) |
entry.place(relx = 0.04 ,rely = 0.17 ) |
|
menuBar = Menu(root) |
root.config(menu = menuBar) |
|
fileMenu = Menu(menuBar, tearoff = 0 ) |
fileMenu.add_command(label = "新建" ) |
menuBar.add_cascade(label = "文件" , menu = fileMenu) |
|
windowMenu = Menu(menuBar, tearoff = 0 ) |
windowMenu.add_command(label = "新建" ) |
windowMenu.add_separator() |
windowMenu.add_command(label = "退出" , command = _quit) |
menuBar.add_cascade(label = "系统" , menu = windowMenu) |
|
helpMenu = Menu(menuBar, tearoff = 0 ) |
helpMenu.add_command(label = "新建" ) |
helpMenu.add_separator() |
helpMenu.add_command(label = "客服" , command = take_help_phono) |
helpMenu.add_command(label = "网址" , command = take_help_http) |
menuBar.add_cascade(label = "帮助" , menu = helpMenu) |
|
commandMenu = Menu(menuBar, tearoff = 0 ) |
commandMenu.add_command(label = "新建" ) |
commandMenu.add_separator() |
commandMenu.add_command(label = "保存翻译内容" , command = saveContent) |
commandMenu.add_command(label = "删除保存的翻译内容文件" , command = delContent) |
commandMenu.add_command(label = "清空翻译历史" , command = delContent_ALL) |
menuBar.add_cascade(label = "命令" , menu = commandMenu) |
|
curRad1 = tk.Radiobutton(root, text = infoList[ 0 ], variable = radVar, value = 0 , bg = "lightgrey" , command = radCall) |
curRad1.place(relx = 0.56 ,rely = 0.4 ) |
curRad2 = tk.Radiobutton(root, text = infoList[ 1 ], variable = radVar, value = 1 , bg = "lightgrey" , command = radCall) |
curRad2.place(relx = 0.8 ,rely = 0.4 ) |
|
scrolW = 30 ; scrolH = 6 |
scr = scrolledtext.ScrolledText(root, width = scrolW, height = scrolH, wrap = tk.WORD) |
scr.place(relx = 0.03 ,rely = 0.36 ) |
|
t = tk.Text(root,height = 1 ,width = 6 ,bg = "lightgrey" ) |
t.place(relx = 0.01 ,rely = 0.95 ) |
t.insert(tk.INSERT, '中译英' ) |
createToolTip(t, '当前翻译状态' ) |
|
enShow = tk.Entry(canvas,font = (fontPath, 15 ),textvariable = v3,state = 'readonly' ).place(relx = 0.58 ,rely = 0.17 ) |
|
createToolTip(translate_B, '翻译内容(请先输入需翻译的内容)' ) |
createToolTip(state_B, '中英翻译状态切换' ) |
createToolTip(play_B, '翻译内容语音播放' ) |
|
root.mainloop() |
中级程序员
by: minecraft 发表于:2022-04-09 12:16:49 顶(0) | 踩(0) 回复
nb
回复评论