话不多说,直接上代码。
Component文件:
# coding:utf-8 from tkinter import * from tkinter import ttk from tkinter import scrolledtext import tkinter.messagebox # 作者:C丶小明 # 博客园地址:https://www.cnblogs.com/mingmingming/ url = "请选择" class Comp: def __init__(self,master): self.master = master def label(self,properties,padx = 3,pady = 2,ipadx = 5,ipady = 1): for name,x,y in properties: self.label = Label(self.master,text = name) self.label.grid(row = x,column = y,padx = padx,pady = pady,ipadx = ipadx,ipady = ipady) def entry(self,properties,padx = 3,pady = 2,ipadx = 5,ipady = 1,columnspan = 4,sticky = N): self.boxes = [] for width,insert,x,y in properties: self.entry = Entry(self.master,width = width) self.entry.insert(END,insert) self.entry.grid(row = x,column = y,columnspan = columnspan,padx = padx,pady = pady,ipadx = ipadx,ipady = ipady,sticky = sticky) self.boxes.append(self.entry) def get_entry(self): self.get_data = [] for box in self.boxes: self.data = box.get() self.get_data.append(self.data) return self.get_data def button(self,properties,padx = 1,pady = 2,ipadx = 2,ipady = 1): for text,x,y,command in properties: self.button = Button(self.master,text = text,command = command) self.button.grid(row = x,column = y,padx = padx,pady = pady,ipadx = ipadx,ipady = ipady) def button_cancel(self): self.master.destroy() def scrolledtext(self,width,height,x,y,padx = 8,pady = 8,ipadx = 5,ipady = 1): self.scrolledtext = scrolledtext.ScrolledText(self.master,width = width,height = height) self.scrolledtext.grid(row = x,column = y,columnspan = 5,padx = padx,pady = pady,ipadx = ipadx,ipady = ipady) def scrolledtext_insert(self,getdata_function): #for data in getdata_function: self.scrolledtext.insert(END,getdata_function) def scrolledtext_delete(self): #for data in getdata_function: self.scrolledtext.delete('1.0', 'end') def combobox(self,values,x,y,width = 35,columnspan = 5,padx = 3,pady = 1,ipadx = 5,ipady = 1,sticky = N): self.select = ttk.Combobox(self.master,width = width,state = 'readonly') # 初始化 self.select['values'] = (values) # 选项 # 处理事件 def combobox_trigger(*args): global url url = self.select.get() #获取选中的值 self.select.set(url) # 下拉框提示文字 #self.select.current(0) # 默认选项 self.select.bind("<<ComboboxSelected>>",combobox_trigger) # 选择触发事件 self.select.grid(row = x, column = y,columnspan = columnspan,padx = padx,pady = pady,ipadx = ipadx,ipady = ipady,sticky = sticky) return url def messagebox(self , features , check1 , check1_return): if check1 in features: tkinter.messagebox.showinfo('提示',check1_return) else: tkinter.messagebox.showwarning('提示','新增失败!') def radiobutton(self,properties,sticky = W): var = IntVar() for text,x,y,value in properties: radiobutton = Radiobutton(self.master, text= text, variable = var, value = value) radiobutton.grid(row = x,column = y ,columnspan = 4,sticky = sticky) radiobutton.flash() if __name__ == "__main__": kk = Tk() c = Comp(kk) c.combobox(("select1","select2"),1,0) kk.mainloop()
TranslationTools文件,翻译小工具的主要部分:
# -*- coding: utf-8 -*- from docx import Document from tkinter import * from googletrans import Translator import tkinter.filedialog import Component # 作者:C丶小明 # 博客园地址:https://www.cnblogs.com/mingmingming/ class Tr: def __init__(self): self.main = Tk() self.mainwindow = Component.Comp(self.main) self.main.geometry('430x400') self.main.title(u"翻译工具 作者:C丶小明") self.main.resizable(width=False,height=False) self.translator = Translator(['translate.google.cn']) def returnselect(self): all_url = ("zh-CN","en") url = self.mainwindow.combobox(("zh-CN","en"),0,1,pady = 25) return False if url == "请选择" else url def interface(self): # 文本 self.mainwindow.label([ ("翻译语言:",0,0), ("选择文档路径:",1,0), ("文档行数:",2,0), ("翻译文档位置:",3,0), ]) # 下拉列表 Language = self.mainwindow.combobox(("zh-CN","en"),0,1,pady = 25) path = StringVar() def selectPath(): # 文件选择框 path_ = tkinter.filedialog.askopenfilename(filetypes=[("",".docx")]) # path = StringVar() 配合使用,更新显示地址 path.set(path_) # 选择文档的路径 t = path_ # 打开文档,空白为新建文档 new_word = Document() document = Document(t) sum_row = len(document.paragraphs) Label(self.main,text = sum_row).grid(row = 2,column = 1,padx = 8) # 循环读取段落 for i in document.paragraphs: # 读取内容,进行翻译 word_text = self.translator.translate(i.text, dest=self.returnselect()).text # 翻译内容写入文档 new_word.add_paragraph(word_text) newpath = r"F:\翻译文档.docx" new_word.save(newpath) Label(self.main,text = newpath).grid(row = 3,column = 1) # 显示选择文件地址 Label(self.main,width =35,height = 3,wraplength = 230,textvariable = path).grid(row = 1,column = 1,padx = 8) # 按钮 self.mainwindow.button([ ("选择",1,2,selectPath) ],ipadx = 8,ipady = 1) self.main.mainloop() if __name__ == "__main__": t = Tr() t.interface()
运行结果: