好久沒發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()


本站作者已申明原創,禁止轉載!

文章內容屬作者個人觀點,不代表本站立場,如有侵權立刪。