from tkinter import * from tkinter import filedialog from tkinter.ttk import Treeview from tkcalendar import Calendar, DateEntry import app import time import param import tkinter.messagebox import sql import output_file import datetime import _thread class my_inquire(Frame): def __init__(self, init_window_name): self.init_window_name = init_window_name self.user_var = app.user_var self.screen_width = self.user_var.screen_width self.screen_height = self.user_var.screen_height self.font_size_title = app.font_size_title self.font_size_text = app.font_size_text self.sqlclient = sql.sqlclient self.main_gui_init() self.title_canvas_init() self.button_canvas_init() self.user_frame_init() self.view_timeout = 180 self.user_var.windows_idx = 'inquire' self.fun_timer() def main_button_event(self): self.user_var.last_windows_idx = 'inquire' self.init_window_name.after_cancel(self.task_user) self.main_frame.destroy() app.my_gui(self.init_window_name) def param_button_event(self): print("Enter Param Set!") self.user_var.last_windows_idx = 'inquire' self.init_window_name.after_cancel(self.task_user) self.main_frame.destroy() param.my_param(self.init_window_name) def authstatlabel_value(self): if self.user_var.auth_stat == False: self.param_button.configure(state="disabled") else: self.param_button.configure(state="normal") def auth_exit(self): if self.user_var.auth_timeout > 0: self.user_var.auth_timeout = self.user_var.auth_timeout - 1 else: self.user_var.auth_stat = False def view_exit(self): if self.view_timeout > 0: self.view_timeout = self.view_timeout - 1 else: self.user_var.last_windows_idx = 'param' self.init_window_name.after_cancel(self.task_user) self.main_frame.destroy() app.my_gui(self.init_window_name) def listview_update(self): if self.user_var.listview_stat == True: self.user_var.listview_stat = False if self.user_var.listview_content == "DATA": self.set_data_listview() elif self.user_var.listview_content == "PARAM": self.set_param_listview() self.user_var.listview_lock = False self.read_data_delay_label("数据已生成。。。", 'green') def windows_update(self): currentTime = time.strftime('%Y/%m/%d %H:%M:%S', time.localtime(time.time())) self.timelabel.config(text=currentTime) self.auth_exit() self.init_window_name.update() self.fun_timer() self.authstatlabel_value() self.view_exit() self.listview_update() def fun_timer(self): self.task_user=self.init_window_name.after(1000, self.windows_update) def main_gui_init(self): self.main_frame = Frame(self.init_window_name, width=self.screen_width, height=self.screen_height) self.main_frame.pack() # 标题栏 def title_canvas_init(self): self.title_canvas = Canvas(self.main_frame, width=self.screen_width - 20, height=35, bg='SeaGreen') self.mainlabel = Label(self.title_canvas, text="气密检查记录查询", fg='SkyBlue', font=("宋体", self.font_size_title), bg='SeaGreen', anchor='c') self.mainlabel.place(x=self.title_canvas.winfo_reqwidth()/2-self.mainlabel.winfo_reqwidth()/2, y=5) self.timelabel = Label(self.title_canvas, text=time.strftime('%Y/%m/%d %H:%M:%S', time.localtime(time.time())), fg='yellow', font=("宋体", self.font_size_title), bg='SeaGreen', anchor='e') self.timelabel.place(x=self.title_canvas.winfo_reqwidth() - self.timelabel.winfo_reqwidth() - 20, y=5) self.title_canvas.create_line(self.mainlabel.winfo_reqwidth() / 2 + 20, 32, self.title_canvas.winfo_reqwidth() - self.timelabel.winfo_reqwidth() / 2 - 20, 32, fill='white') self.title_canvas.place(x=10, y=10) # 控制栏 def button_canvas_init(self): self.button_canvas = Canvas(self.main_frame, width=self.screen_width - 20, height=40) self.main_button = Button(self.button_canvas, text="主界面", fg='white', bg='DeepSkyBlue', width=8, activebackground='white', activeforeground='DeepSkyBlue', font=("宋体", self.font_size_title, "bold"), command=self.main_button_event) self.main_button.place(x=1, y=0) self.param_button = Button(self.button_canvas, text="参数设定", fg='white', bg='DeepSkyBlue', width=8, activebackground='white', activeforeground='DeepSkyBlue', state="disabled", font=("宋体", self.font_size_title, "bold"), command=self.param_button_event) self.param_button.place(x=110, y=0) self.delaylabel = Label(self.button_canvas, fg='green', font=("宋体", self.font_size_title, "bold"), text="无数据。。。", anchor='w', ) self.delaylabel.place(x=self.button_canvas.winfo_reqwidth() - 200, y=10) self.button_canvas.place(x=10, y=self.screen_height - 60) def return_button_event(self): if self.user_var.last_windows_idx == 'param': self.user_var.last_windows_idx = 'production' self.init_window_name.after_cancel(self.task_user) self.main_frame.destroy() param.my_param(self.init_window_name) else: self.user_var.last_windows_idx = 'production' self.init_window_name.after_cancel(self.task_user) self.main_frame.destroy() app.my_gui(self.init_window_name) def get_begin_data_event(self, event): if self.cal_begin.get_date() <= self.cal_end.get_date(): self.user_var.inquire_begin_data = self.cal_begin.get_date() print(self.user_var.inquire_begin_data) else: tkinter.messagebox.showwarning(title='提示', message='开始日期大于结束日子!') self.cal_begin.set_date(self.user_var.inquire_begin_data) def get_end_data_event(self, event): if self.cal_begin.get_date() <= self.cal_end.get_date(): self.user_var.inquire_end_data = self.cal_end.get_date() print(self.user_var.inquire_end_data) else: tkinter.messagebox.showwarning(title='提示', message='开始日期大于结束日子!') self.cal_end.set_date(self.user_var.inquire_end_data) def airparam_button_event(self): print("气密参数查询!") if self.user_var.listview_lock == False: self.user_var.file_name = "气密参数" self.user_var.listview_content = "PARAM" self.excel_button.configure(state="normal") self.pdf_button.configure(state="normal") self.clear_listview() thread_savefile = output_file.output_file(10, "Thread-output", 3, 'inquire_param') thread_savefile.start() self.read_data_delay_label("数据生成中。。。", 'red') self.user_var.listview_lock = True else: tkinter.messagebox.showwarning(title='提示', message='数据查询中。。。') def thatday_button_event(self): print("当天数据查询!") if self.user_var.listview_lock == False: self.user_var.file_name = "当天数据{0}".format(datetime.datetime.now().strftime('%Y%m%d%H%M%S')) self.user_var.listview_content = "DATA" self.excel_button.configure(state="normal") self.pdf_button.configure(state="normal") self.clear_listview() now_time = int(time.time()) day_time = now_time - now_time % 86400 wherestr = "WHERE end_time >= {0} and end_time <= {1}".format(day_time, day_time + 86400) thread_savefile = output_file.output_file(10, "Thread-output", 3, 'inquire_data', datastr=wherestr) thread_savefile.start() self.read_data_delay_label("数据生成中。。。", 'red') self.user_var.listview_lock = True else: tkinter.messagebox.showwarning(title='提示', message='数据查询中。。。') def excel_button_event(self): print("导出excel!") if self.user_var.file_save_stat == True: fn = filedialog.asksaveasfilename(title='另存为', initialfile=self.user_var.file_name, defaultextension=".xls", filetypes=[('xls文件', '.xls')]) if fn: content = self.user_var.product_data.copy() if self.user_var.listview_content == "DATA": content.insert(0, self.user_var.data_headers) elif self.user_var.listview_content == "PARAM": content.insert(0, self.user_var.param_headers) self.user_var.file_save_stat = False thread_savefile = output_file.output_file(10, "Thread-output", 3, 'save_excel', filepath=fn, content=content) thread_savefile.start() else: tkinter.messagebox.showwarning(title='提示', message='文件生成中!') def clear_button_event(self): print("清除显示!") if self.user_var.listview_lock == False: self.user_var.listview_content = "NULL" self.clear_listview() self.excel_button.configure(state="disable") self.pdf_button.configure(state="disable") self.read_data_delay_label("无数据。。。", 'green') def days_button_event(self): print("多天数据!") if self.user_var.listview_lock == False: self.user_var.listview_content = "DATA" self.excel_button.configure(state="normal") self.pdf_button.configure(state="normal") self.clear_listview() begintime = int(time.mktime(time.strptime("{0} 00:00:00".format( self.user_var.inquire_begin_data), "%Y-%m-%d %H:%M:%S"))) endtime = int(time.mktime(time.strptime("{0} 23:59:59".format( self.user_var.inquire_end_data), "%Y-%m-%d %H:%M:%S"))) self.user_var.file_name = "多天数据{0}-{1}".format(time.strftime("%Y%m%d", time.localtime(begintime)), time.strftime("%Y%m%d", time.localtime(endtime))) wherestr = "WHERE end_time >= {0} and end_time <= {1}".format(begintime, endtime) thread_savefile = output_file.output_file(10, "Thread-output", 3, 'inquire_data', datastr=wherestr) thread_savefile.start() self.read_data_delay_label("数据生成中。。。", 'red') self.user_var.listview_lock = True else: tkinter.messagebox.showwarning(title='提示', message='数据查询中。。。') def all_button_event(self): print("全部数据!") if self.user_var.listview_lock == False: self.user_var.file_name = "全部数据{0}".format(datetime.datetime.now().strftime('%Y%m%d%H%M%S')) self.user_var.listview_content = "DATA" self.excel_button.configure(state="normal") self.pdf_button.configure(state="normal") self.clear_listview() thread_savefile = output_file.output_file(10, "Thread-output", 3, 'inquire_data') thread_savefile.start() self.read_data_delay_label("数据生成中。。。", 'red') self.user_var.listview_lock = True else: tkinter.messagebox.showwarning(title='提示', message='数据查询中。。。') def pdf_button_event(self): print("导出pdf!") if self.user_var.file_save_stat == True: fn = filedialog.asksaveasfilename(title='另存为', initialfile=self.user_var.file_name, defaultextension=".pdf", filetypes=[('pdf文件', '.pdf')]) if fn: content = self.user_var.product_data.copy() if self.user_var.listview_content == "DATA": content.insert(0, self.user_var.data_headers) elif self.user_var.listview_content == "PARAM": content.insert(0, self.user_var.param_headers) self.user_var.file_save_stat = False thread_savefile = output_file.output_file(10, "Thread-output", 3, 'save_pdf', filepath=fn, content=content) thread_savefile.start() else: tkinter.messagebox.showwarning(title='提示', message='文件生成中!') def label_button_event(self): print("标签查询!") if self.user_var.listview_lock == False: self.user_var.listview_content = "DATA" txt = self.labeltext.get().strip() self.labeltext.config(text=txt) if txt != '' and len(txt) == 12: self.excel_button.configure(state="normal") self.pdf_button.configure(state="normal") self.clear_listview() wherestr = "WHERE batch = '{0}'".format(txt) thread_savefile = output_file.output_file(10, "Thread-output", 3, 'inquire_data', datastr=wherestr) thread_savefile.start() self.user_var.listview_lock = True self.user_var.file_name = "标签数据txt" else: tkinter.messagebox.showwarning(title='提示', message='请输入12位标签号!') else: tkinter.messagebox.showwarning(title='提示', message='数据查询中。。。') def read_data_delay_label(self, txt, color): self.delaylabel.configure(text=txt) self.delaylabel.configure(fg=color) def clear_listview(self): for widget in self.treeview_frame.winfo_children(): widget.destroy() def set_param_listview(self): widths = (100, 130, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80) # 构建控件 data_listview = Treeview(self.treeview_frame, columns=self.user_var.param_columns, show='headings') data_listview.column("#0", width=2, minwidth=1, stretch=YES) idx =1 for column in self.user_var.param_columns: data_listview.column('#{0}'.format(idx), width=widths[idx - 1], minwidth=widths[idx - 1], stretch=YES, anchor=CENTER) idx = idx + 1 idx = 1 for header in self.user_var.param_headers: data_listview.heading('#{0}'.format(idx), text='{0}'.format(header), anchor=CENTER) idx = idx + 1 for i, person in enumerate(self.user_var.product_data): data_listview.insert('', i, values=person) # 构建垂直Scrollbar scroll_y = Scrollbar(self.treeview_frame, orient=VERTICAL, command=data_listview.yview) # 设定控件布局 scroll_y.pack(side='right', fill='y', padx=1, pady=1, ipadx=1, ipady=1) # 绑定滚动动作 data_listview.configure(yscrollcommand=scroll_y.set) # 构建水平Scrollbar scroll_x = Scrollbar(self.treeview_frame, orient=HORIZONTAL, command=data_listview.xview) # 设定控件布局 scroll_x.pack(side='bottom', fill='x', padx=1, pady=1, ipadx=1, ipady=1) # 绑定滚动动作 data_listview.configure(xscrollcommand=scroll_x.set) # 设定控件布局 data_listview.pack(side='left', fill='both', padx=1, pady=1, ipadx=1, ipady=1) def set_data_listview(self): widths = (80, 60, 100, 100, 60, 250, 50, 130, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80) # 构建控件 data_listview = Treeview(self.treeview_frame, columns=self.user_var.data_columns, show='headings') data_listview.column("#0", width=2, minwidth=1, stretch=YES) idx =1 for column in self.user_var.data_columns: data_listview.column('#{0}'.format(idx), width=widths[idx - 1], minwidth=widths[idx - 1], stretch=YES, anchor=CENTER) idx = idx + 1 idx = 1 for header in self.user_var.data_headers: data_listview.heading('#{0}'.format(idx), text='{0}'.format(header), anchor=CENTER) idx = idx + 1 for i, person in enumerate(self.user_var.product_data): data_listview.insert('', i, values=person) # 构建垂直Scrollbar scroll_y = Scrollbar(self.treeview_frame, orient=VERTICAL, command=data_listview.yview) # 设定控件布局 scroll_y.pack(side='right', fill='y', padx=1, pady=1, ipadx=1, ipady=1) # 绑定滚动动作 data_listview.configure(yscrollcommand=scroll_y.set) # 构建水平Scrollbar scroll_x = Scrollbar(self.treeview_frame, orient=HORIZONTAL, command=data_listview.xview) # 设定控件布局 scroll_x.pack(side='bottom', fill='x', padx=1, pady=1, ipadx=1, ipady=1) # 绑定滚动动作 data_listview.configure(xscrollcommand=scroll_x.set) # 设定控件布局 data_listview.pack(side='left', fill='both', padx=1, pady=1, ipadx=1, ipady=1) # 输入栏 def user_frame_init(self): self.order_frame = Frame( self.main_frame, width=self.screen_width - 20, relief=SUNKEN, borderwidth=2, height=self.screen_height - self.title_canvas.winfo_reqheight() - self.button_canvas.winfo_reqheight() - 35) # 工单栏展示 self.order_frame.place(x=12, y=self.title_canvas.winfo_reqheight() + 15) self.operate_frame = Frame( self.order_frame, width=990, borderwidth=0, height=110) self.operate_frame.pack(side='top', padx=1, pady=1, ipadx=2, ipady=2) self.return_button_canvas = Canvas(self.operate_frame, bg='DarkGray', relief=SOLID, borderwidth=1, width=117, height=33) self.return_button = Button(self.return_button_canvas, text="返回", fg='black', bg='white', width=11, activebackground='black', activeforeground='white', font=("宋体", self.font_size_text, "bold"), command=self.return_button_event) self.return_button.place(x=7, y=7) self.return_button_canvas.place(x=50, y=5) self.airparam_button_canvas = Canvas(self.operate_frame, bg='DarkGray', relief=SOLID, borderwidth=1, width=117, height=33) self.airparam_button = Button(self.airparam_button_canvas, text="气密参数查询", fg='black', bg='white', width=11, activebackground='black', activeforeground='white', font=("宋体", self.font_size_text, "bold"), command=self.airparam_button_event) self.airparam_button.place(x=7, y=7) self.airparam_button_canvas.place(x=170, y=5) self.thatday_button_canvas = Canvas(self.operate_frame, bg='DarkGray', relief=SOLID, borderwidth=1, width=117, height=33) self.thatday_button = Button(self.thatday_button_canvas, text="当天数据", fg='black', bg='white', width=11, activebackground='black', activeforeground='white', font=("宋体", self.font_size_text, "bold"), command=self.thatday_button_event) self.thatday_button.place(x=7, y=7) self.thatday_button_canvas.place(x=290, y=5) self.excel_button_canvas = Canvas(self.operate_frame, bg='DarkGray', relief=SOLID, borderwidth=1, width=117, height=33) self.excel_button = Button(self.excel_button_canvas, text="导出数据", fg='black', bg='white', width=11, activebackground='black', activeforeground='white', state="disabled", font=("宋体", self.font_size_text, "bold"), command=self.excel_button_event) self.excel_button.place(x=7, y=7) self.excel_button_canvas.place(x=410, y=5) self.clear_button_canvas = Canvas(self.operate_frame, bg='DarkGray', relief=SOLID, borderwidth=1, width=117, height=33) self.clear_button = Button(self.clear_button_canvas, text="清除显示", fg='black', bg='white', width=11, activebackground='black', activeforeground='white', font=("宋体", self.font_size_text, "bold"), command=self.clear_button_event) self.clear_button.place(x=7, y=7) self.clear_button_canvas.place(x=50, y=40) self.days_button_canvas = Canvas(self.operate_frame, bg='DarkGray', relief=SOLID, borderwidth=1, width=117, height=33) self.days_button = Button(self.days_button_canvas, text="多天数据", fg='black', bg='white', width=11, activebackground='black', activeforeground='white', font=("宋体", self.font_size_text, "bold"), command=self.days_button_event) self.days_button.place(x=7, y=7) self.days_button_canvas.place(x=170, y=40) self.all_button_canvas = Canvas(self.operate_frame, bg='DarkGray', relief=SOLID, borderwidth=1, width=117, height=33) self.all_button = Button(self.all_button_canvas, text="全部数据", fg='black', bg='white', width=11, activebackground='black', activeforeground='white', font=("宋体", self.font_size_text, "bold"), command=self.all_button_event) self.all_button.place(x=7, y=7) self.all_button_canvas.place(x=290, y=40) self.pdf_button_canvas = Canvas(self.operate_frame, bg='DarkGray', relief=SOLID, borderwidth=1, width=117, height=33) self.pdf_button = Button(self.pdf_button_canvas, text="导出pdf", fg='black', bg='white', width=11, activebackground='black', activeforeground='white', state="disabled", font=("宋体", self.font_size_text, "bold"), command=self.pdf_button_event) self.pdf_button.place(x=7, y=7) self.pdf_button_canvas.place(x=410, y=40) self.labeltitle = Label(self.operate_frame, text="输入查询的标签", font=("宋体", self.font_size_text), anchor='e') self.labeltitle.place(x=self.operate_frame.winfo_reqwidth() - self.labeltitle.winfo_reqwidth() - 50, y=5) self.labeltext = Entry(self.operate_frame, font=("宋体", self.font_size_text), fg="darkgoldenrod", bg="SkyBlue", width=20, justify='center') self.labeltext.place(x=self.operate_frame.winfo_reqwidth() - self.labeltext.winfo_reqwidth() - 28, y=30) self.label_button_canvas = Canvas(self.operate_frame, bg='DarkGray', relief=SOLID, borderwidth=1, width=117, height=33) self.label_button = Button(self.label_button_canvas, text="查看label", fg='black', bg='white', width=11, activebackground='black', activeforeground='white', font=("宋体", self.font_size_text, "bold"), command=self.label_button_event) self.label_button.place(x=7, y=7) self.label_button_canvas.place( x=self.operate_frame.winfo_reqwidth() - self.label_button_canvas.winfo_reqwidth() - 40, y=55) self.datatitlelabel = Label(self.operate_frame, text="气密数据查询列表", fg='dodgerblue', font=("宋体", self.font_size_text), anchor='w') self.datatitlelabel.place(x=self.operate_frame.winfo_reqwidth() / 2 - self.datatitlelabel.winfo_reqwidth() / 2, y=80) self.calbegin_canvas = Canvas(self.operate_frame, bg='white', relief=SOLID, borderwidth=1, width=180, height=33) self.calbegintitlelabel = Label(self.calbegin_canvas, text="起始:", fg='dodgerblue', bg='white', font=("宋体", self.font_size_text), anchor='w') self.calbegintitlelabel.place(x=10, y=10) self.cal_begin = DateEntry(self.calbegin_canvas, locale='zh_CN', width=12, background='darkblue', foreground='white', borderwidth=2) self.cal_begin.place(x=65, y=8) self.cal_begin.bind("<>", self.get_begin_data_event) self.calbegin_canvas.place( x=550, y=5) self.calend_canvas = Canvas(self.operate_frame, bg='white', relief=SOLID, borderwidth=1, width=180, height=33) self.calendtitlelabel = Label(self.calend_canvas, text="结束:", fg='dodgerblue', bg='white', font=("宋体", self.font_size_text), anchor='w') self.calendtitlelabel.place(x=10, y=10) self.cal_end = DateEntry(self.calend_canvas, locale='zh_CN', width=12, background='darkblue', foreground='white', borderwidth=2) self.cal_end.place(x=65, y=8) self.cal_end.bind("<>", self.get_end_data_event) self.calend_canvas.place( x=550, y=40) self.treeview_frame = Frame(self.order_frame, bg='white', relief=SOLID, borderwidth=1, width=990, height=350) self.treeview_frame.pack(side='bottom', padx=2, pady=2, ipadx=2, ipady=2) self.treeview_frame.pack_propagate(0) if self.user_var.listview_content == "DATA": self.excel_button.configure(state="normal") self.pdf_button.configure(state="normal") self.set_data_listview() self.read_data_delay_label("数据已生成。。。", 'green') elif self.user_var.listview_content == "PARAM": self.excel_button.configure(state="normal") self.pdf_button.configure(state="normal") self.set_param_listview() self.read_data_delay_label("数据已生成。。。", 'green') # t = time.time() # # print (t) #原始时间数据 # print (int(t)) #秒级时间戳 # print (int(round(t * 1000))) #毫秒级时间戳 # print (int(round(t * 1000000))) #微秒级时间戳 # dt = '2018-01-01 10:40:30' # ts = int(time.mktime(time.strptime(dt, "%Y-%m-%d %H:%M:%S"))) # print (ts) # ts = 1515774430 # dt = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(ts)) # print(dt)