好久没发python程序了,今天再发一个
注:要先安装pyttsx3和requests
上代码:
import requests
import pyttsx3,tkinter
from tkinter import messagebox,simpledialog
import tkinter
class Trans:
url="http://fanyi.youdao.com/translate"
def translate(self,string):
try:
data = {
'doctype': 'json',
'type': 'AUTO',
'i': string
}
r = requests.get(self.url, params=data)
result = r.json()
return result['translateResult'][0][0]["tgt"]
except:
raise Exception('TranslateError')
def say(string):
engine = pyttsx3.init()
engine.say(string)
engine.runAndWait()
def translate():
global s,r
s=entry.get()
t=Trans()
try:
r=t.translate(s)
messagebox.showinfo('翻译','翻译成功!\n结果是:'+str(r))
except:
messagebox.showerror('错误','翻译错误!请检查网络和Internet状态!如果网络良好,请检查输入文字!')
def sayr():
try:
say(r)
except:
pass
def says():
try:
say(s)
except:
pass
def clear():
entry.delete(0,tkinter.END)
def fquit():
if messagebox.askquestion('退出','是否退出翻译器?') =='yes':
window.destroy()
def about():
messagebox.showinfo('About','欢迎使用I Love Scratch翻译机\n版本3.2.5\n基于有道翻译')
window=tkinter.Tk()
window.title('翻译')
window.geometry("300x200+100+10")
window.resizable(width = False,height = False)
def cut():
textc = entry.get()
entry.delete(tkinter.SEL_FIRST, tkinter.SEL_LAST)
entry.clipboard_clear()
entry.clipboard_append(textc)
#6复制
def copy():
textc = entry.get()
entry.clipboard_clear()
entry.clipboard_append(textc)
#7粘贴
def paste():
try:
textp = entry.selection_get(selection="CLIPBOARD")
entry.insert(tkinter.INSERT, textp)
entry.clipboard_clear()
except tkinter.TclError:
pass #不做任何事情
def copyrightTrans():
messagebox.showinfo('Copyright','Translation machine\nCopyrightⓒ2022 I Love Scratch\nAll rights reserved')
def suggestion():
a = simpledialog.askstring('建议','请写下你的建议')
try:
f=open('翻译的意见.txt',mode = 'a+')
f.write(a,'\n')
f.close()
messagebox.showinfo('恭喜','反馈成功')
except:
messagebox.showerror('错误','反馈失败')
menubar = tkinter.Menu(window)
fmenu = tkinter.Menu(menubar, tearoff=False)
fmenu.add_command(label="翻译", command=translate)
fmenu.add_command(label="朗读", command=says)
fmenu.add_command(label="清空", command=clear)
fmenu.add_separator()#分割线
fmenu.add_command(label="退出", command=fquit)
vmenu = tkinter.Menu(menubar, tearoff=False)
vmenu.add_command(label='剪切', accelerator='Ctrl + X', command=cut)
vmenu.add_command(label="复制",accelerator='Ctrl + C',command=copy)
vmenu.add_command(label="粘贴",accelerator="Ctrl + V",command=paste)
amenu = tkinter.Menu(menubar, tearoff=False)
amenu.add_command(label="关于翻译", command=about)
amenu.add_command(label="版权信息", command=copyrightTrans)
amenu.add_command(label="提出建议", command=suggestion)
menubar.add_cascade(label='翻译', menu=fmenu)
menubar.add_cascade(label='编辑', menu=vmenu)
menubar.add_cascade(label='关于', menu=amenu)
window['menu']=menubar
entry=tkinter.Entry(window)
lab1=tkinter.Label(window,text='请输入要翻译的内容')
btn1=tkinter.Button(text='翻译',command=translate)
btn2=tkinter.Button(text='朗读要翻译的内容',command=says)
btn3=tkinter.Button(text='朗读翻译结果',command=sayr)
lab1.pack()
entry.pack()
btn1.pack()
btn2.pack()
btn3.pack()
window.mainloop()
本站作者已申明原创,禁止转载!
文章内容属作者个人观点,不代表本站立场,如有侵权立删。






