from tkinter import *
from tkinter import messagebox
from tkinter import ttk
import pyperclip
import threading
from tkinter.filedialog import askopenfilename
from Translator import Do_Trans
"""
使用combobox進行語言記錄
"""
class App:
def __init__(self):
self.window=Tk()
self.window.title('翻譯器-v2.0')
width=480
height=480
left=(self.window.winfo_screenwidth()-width)/2
top=(self.window.winfo_screenheight()-height)/2
self.window.geometry('%dx%d+%d+%d'%(width,height,left,top))
self.window.resizable(0,0)
self.create_widget()
self.set_widget()
self.place_widget()
self.language='auto'
self.window.mainloop()
def create_widget(self):
self.l1=ttk.Label(self.window)
self.t1=Text(self.window)
self.b1=ttk.Button(self.window)
self.b2=ttk.Button(self.window)
self.b3=ttk.Button(self.window)
self.l2=ttk.Label(self.window)
self.t2=Text(self.window)
self.Scroll_vertical1 = Scrollbar(self.window, orient=VERTICAL)
self.Scroll_vertical2 = Scrollbar(self.window, orient=VERTICAL)
self.Scroll_level = Scrollbar(self.window, orient=HORIZONTAL)
self.m=Menu(self.window)
self.window['menu']=self.m
self.l3=ttk.Label(self.window)
def set_widget(self):
self.l1.config(text='待翻譯文本',font=('宋體',10))
self.b1.config(text='清空輸入框',command=lambda:self.thread_it(self.clear_t))
self.b2.config(text='翻譯',command=lambda:self.thread_it(self.do_translate))
self.b3.config(text='復制翻譯內(nèi)容',command=lambda:self.thread_it(self.copy_t))
self.l2.config(text='翻譯結(jié)果',font=('宋體',10))
self.Scroll_vertical1.config(command=self.t1.yview)
self.t1['yscrollcommand'] = self.Scroll_vertical1.set
self.Scroll_vertical2.config(command=self.t2.yview)
self.t2['yscrollcommand'] = self.Scroll_vertical2.set
self.t2['xscrollcommand']=self.Scroll_level.set
self.s1=Menu(self.m,tearoff=False)
self.s2=Menu(self.m,tearoff=False)
self.s3=Menu(self.m,tearoff=False)
self.m.add_cascade(label='文件',menu=self.s1)
self.m.add_cascade(label='操作',menu=self.s2)
self.m.add_cascade(label='關(guān)于',menu=self.s3)
self.s1.add_command(label='打開文本文件',command=self.open_txt)
self.s1.add_separator()
self.s1.add_command(label='退出',command=self.quit_window)
self.s2.add_command(label='翻譯',command=lambda:self.thread_it(self.do_translate))
self.s2.add_command(label='選擇語言',command=self.open_topleval)
self.s2.add_command(label='清空內(nèi)容',command=self.clear_t)
self.s2.add_command(label='復制結(jié)果',command=lambda:self.thread_it(self.copy_t))
self.s3.add_command(label='說明',command=self.show_infos)
self.l3_var=StringVar()
self.l3.config(textvariable=self.l3_var,background='lightblue')
self.l3_var.set('當前[自動選擇]目標語言')
self.window.bind('Escape>',self.escape)
self.t1.bind('Return>',lambda:self.thread_it(self.do_translate))
self.window.protocol('WM_DELETE_WINDOW',self.quit_window)
self.menubar = Menu(self.t1,tearoff=False)
self.menubar.add_command(label="粘貼", command=self.do_paste)
self.t1.bind('Button-3>',self.paste)
self.menubar2 = Menu(self.t2,tearoff=False)
self.menubar2.add_command(label="粘貼", command=self.do_paste2)
self.t2.bind('Button-3>',self.paste2)
self.current_select=0
def place_widget(self):
self.l1.place(x=200,y=5)
self.t1.place(x=10,y=30,height=150,width=450)
self.Scroll_vertical1.place(x=445,y=30,height=150)
self.b1.place(x=10,y=200)
self.b2.place(x=180,y=200)
self.b3.place(x=370,y=200)
self.l2.place(x=200,y=228)
self.t2.place(x=10,y=250,height=180,width=450)
self.Scroll_vertical2.place(x=445,y=250,height=180)
self.Scroll_level.place(x=10,y=430,width=450)
self.l3.place(x=0,y=450,width=480,height=30)
def open_topleval(self):
self.select_lan_window=Toplevel()
width=250
height=50
left=(self.select_lan_window.winfo_screenwidth()-width)/2
top=(self.select_lan_window.winfo_screenheight()-height)/2
self.select_lan_window.geometry('%dx%d+%d+%d'%(width,height,left,top))
self.select_lan_window.resizable(0,0)
self.s_combobox_var=StringVar()
self.language_table=Do_Trans().get_language_table()
self.s_combobox=ttk.Combobox(self.select_lan_window,textvariable=self.s_combobox_var,justify='center',state='readonly',width=17,value=[language['language'] for language in self.language_table])
#對當前選中語言進行記錄
if self.current_select==0:
self.s_combobox.current(0)
else:
self.s_combobox.current(self.current_select)
self.s_b1=ttk.Button(self.select_lan_window,text='選擇',command=self.select_lan)
self.s_combobox.pack(side=LEFT)
self.s_b1.pack(side=RIGHT)
self.select_lan_window.mainloop()
def select_lan(self):
self.current_select=self.s_combobox.current()
self.now_language=self.language_table[self.s_combobox.current()]['language']
messagebox.showinfo('提示',f'當前選擇 [{self.now_language}] 作為目標語言')
self.l3_var.set(f'選擇[{self.now_language}]作為目標語言')
self.now_lan=self.language_table[self.s_combobox.current()]['short']
self.select_lan_window.destroy()
def open_txt(self):
txt_path = askopenfilename(title='選擇一個txt文本文件',filetypes=[('txt source file','*.txt')])
if txt_path:
self.t1.delete('0.0',END)
with open(txt_path,'r',encoding='utf-8')as f:
for line in f.readlines():
self.t1.insert(END,line)
f.close()
def paste(self,event):
self.menubar.post(event.x_root,event.y_root)
def do_paste(self):
self.t1.insert(END,pyperclip.paste())
def paste2(self,event):
self.menubar2.post(event.x_root,event.y_root)
def do_paste2(self):
self.t2.insert(END,pyperclip.paste())
def escape(self,event):
self.quit_window()
def quit_window(self):
ret=messagebox.askyesno('退出','是否要退出?')
if ret:
self.window.destroy()
def do_translate(self):
try:
self.aim_language=self.now_lan
except AttributeError:
self.aim_language= self.language
self.t2.delete('0.0',END)
text=self.t1.get('0.0',END)
if len(text)!=1:
self.l3_var.set('正在翻譯...')
t=Do_Trans()
result=t.translate(text,self.aim_language)
if result:
self.l3.config(background='lightblue')
self.t2.insert(END,result.strip())
try:
self.l3_var.set(f'翻譯完成...已翻譯為[{self.now_language}]')
except AttributeError:
self.l3_var.set(f'翻譯完成...已[自動選擇目標語言]')
else:
self.l3_var.set('翻譯失敗,請檢查網(wǎng)絡(luò)!')
self.l3.config(background='red')
else:
messagebox.showwarning('警告','請輸入內(nèi)容!')
self.l3.config(background='red')
self.l3_var.set('請輸入內(nèi)容')
def clear_t(self):
self.t1.delete('0.0',END)
self.t2.delete('0.0',END)
def copy_t(self):
tr_res=self.t2.get('0.0',END)
pyperclip.copy(tr_res)
spam = pyperclip.paste()
if spam:
self.l3.config(background='lightyellow')
self.l3_var.set('復制成功!')
def show_infos(self):
messagebox.showinfo('說明','作者:懷淰メ')
def thread_it(self,func,*args):
t=threading.Thread(target=func,args=args)
t.setDaemon(True)
t.start()
if __name__ == '__main__':
a=App()
以上就是python 制作一個gui的翻譯工具的詳細內(nèi)容,更多關(guān)于python gui翻譯工具的資料請關(guān)注腳本之家其它相關(guān)文章!