[Python] 纯文本查看 复制代码
from docxtpl import DocxTemplate
from tkinter import *
from tkinter.ttk import *
import time
import os, sys
class WinGUI(Tk):
widget_dic = dict()
def __init__(self):
super().__init__()
self.__win()
self.widget_dic["tk_label_num"] = self.__tk_label_num(self)
self.widget_dic["tk_label_subject"] = self.__tk_label_subject(self)
self.widget_dic["tk_label_inscription"] = self.__tk_label_inscription(self)
self.widget_dic["tk_label_strdate"] = self.__tk_label_strdate(self)
self.widget_dic["tk_label_context"] = self.__tk_label_context(self)
self.widget_dic["tk_input_num"] = self.__tk_input_num(self)
self.widget_dic["tk_input_subject"] = self.__tk_input_subject(self)
self.widget_dic["tk_input_inscription"] = self.__tk_input_inscription(self)
self.widget_dic["tk_input_strdate"] = self.__tk_input_strdate(self)
self.widget_dic["tk_text_context"] = self.__tk_text_context(self)
self.widget_dic["tk_button_lglldrmn"] = self.__tk_button_lglldrmn(self)
self.widget_dic["tk_label_lglle6t3"] = self.__tk_label_lglle6t3(self)
self.widget_dic["tk_button_default"] = self.__tk_button_default(self)
self.widget_dic["tk_button_openmoban"] = self.__tk_button_openmoban(self)
def __win(self):
self.title("一键套红")
# 设置窗口大小、居中
width = 600
height = 500
screenwidth = self.winfo_screenwidth()
screenheight = self.winfo_screenheight()
geometry = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
self.geometry(geometry)
self.resizable(width=False, height=False)
# 自动隐藏滚动条
def scrollbar_autohide(self, bar, widget):
self.__scrollbar_hide(bar, widget)
widget.bind("<Enter>", lambda e: self.__scrollbar_show(bar, widget))
bar.bind("<Enter>", lambda e: self.__scrollbar_show(bar, widget))
widget.bind("<Leave>", lambda e: self.__scrollbar_hide(bar, widget))
bar.bind("<Leave>", lambda e: self.__scrollbar_hide(bar, widget))
def __scrollbar_show(self, bar, widget):
bar.lift(widget)
def __scrollbar_hide(self, bar, widget):
bar.lower(widget)
def __tk_label_num(self, parent):
label = Label(parent, text="文号", anchor="center")
label.place(x=60, y=20, width=50, height=30)
return label
def __tk_label_subject(self, parent):
label = Label(parent, text="标题", anchor="center")
label.place(x=60, y=60, width=50, height=30)
return label
def __tk_label_inscription(self, parent):
label = Label(parent, text="落款", anchor="center")
label.place(x=60, y=100, width=50, height=30)
return label
def __tk_label_strdate(self, parent):
label = Label(parent, text="日期", anchor="center")
label.place(x=60, y=140, width=50, height=30)
return label
def __tk_label_context(self, parent):
label = Label(parent, text="内容", anchor="center")
label.place(x=60, y=180, width=50, height=30)
return label
def __tk_input_num(self, parent):
ipt = Entry(parent)
ipt.place(x=120, y=20, width=389, height=30)
return ipt
def __tk_input_subject(self, parent):
ipt = Entry(parent)
ipt.place(x=120, y=60, width=389, height=30)
return ipt
def __tk_input_inscription(self, parent):
ipt = Entry(parent)
ipt.place(x=120, y=100, width=389, height=30)
return ipt
def __tk_input_strdate(self, parent):
ipt = Entry(parent)
ipt.place(x=120, y=140, width=389, height=30)
return ipt
def __tk_text_context(self, parent):
text = Text(parent)
text.place(x=120, y=180, width=389, height=203)
return text
def __tk_button_lglldrmn(self, parent):
btn = Button(parent, text="开始套红")
btn.place(x=130, y=420, width=341, height=30)
return btn
def __tk_label_lglle6t3(self, parent):
label = Label(parent, text="套红完毕的文件生成在程序所在的文件夹", anchor="center")
label.place(x=60, y=460, width=482, height=30)
return label
def __tk_button_default(self, parent):
btn = Button(parent, text="默认值")
btn.place(x=60, y=420, width=63, height=30)
return btn
def __tk_button_openmoban(self, parent):
btn = Button(parent, text="打开模版")
btn.place(x=480, y=420, width=62, height=30)
return btn
class Win(WinGUI):
def __init__(self):
super().__init__()
self.__event_bind()
def taohong(self, evt):
base_path = self.base_path('')
tpl = DocxTemplate(base_path+'moban.docx')
num = self.widget_dic["tk_input_num"].get()
subject = self.widget_dic["tk_input_subject"].get()
context = self.widget_dic["tk_text_context"].get(1.0, END)
inscription = self.widget_dic["tk_input_inscription"].get()
strdate = self.widget_dic["tk_input_strdate"].get()
strlist = {"num": num, "subject": subject, "context": context, "inscription": inscription, "strdate": strdate}
tpl.render(strlist)
tpl.save(subject+".docx")
self.widget_dic["tk_label_lglle6t3"]["text"]= "套红完毕!"
os.system('start ' +subject+ '.docx')
def default(self, evt):
nowtime = time.localtime(time.time())
self.widget_dic["tk_input_num"].insert(0,"XXXXX党总支委发〔"+str(nowtime[0])+"〕1号")
self.widget_dic["tk_input_inscription"].insert(0,"XXXXXXX有限公司总支部委员会")
self.widget_dic["tk_input_strdate"].insert(0,str(nowtime[0])+"年"+str(nowtime[1])+"月"+str(nowtime[2])+"日")
print(self.base_path(''))
def openmoban(self, evt):
base_path = self.base_path('')
os.system('start '+base_path+ 'moban.docx')
def __event_bind(self):
self.widget_dic["tk_button_lglldrmn"].bind('<Button-1>', self.taohong)
self.widget_dic["tk_button_default"].bind('<Button-1>', self.default)
self.widget_dic["tk_button_openmoban"].bind('<Button-1>', self.openmoban)
def base_path(self,path):
if getattr(sys, 'frozen', None):
basedir = sys._MEIPASS
else:
basedir = os.path.dirname(__file__)
return os.path.join(basedir, path)
if __name__ == "__main__":
win = Win()
win.mainloop()