一个项目的tkinter 部分
import tkinter as tk from tkinter import filedialog import time from tkinter import messagebox import excel_app.excel_reader as er from excel_app.excel_reader import FileError def open_file(): """ 导入文件路径 :return: 返回sheet名列表 """ global textvar global filepath global num_filepaths var1.set("")
# 创建一个导入文件的功能,askopenfilename:只能导入一个文件,askopenfilenames:可以导入多个文件, filetypes:可以筛选导入文件的类型 filepaths = filedialog.askopenfilenames(title=u'导入文件', filetypes=[("excel files", ".xlsx")]) num_filepaths = len(filepaths) if not filepaths: text_update("你没有选择任何文件", color="red") elif num_filepaths == 1: filepath = filepaths[0] text_update("导入文件%s" % filepath) sheetnames = er.sheet_select(filepath) var1.set(sheetnames) else: for filepath in filepaths: text_update("导入文件%s" % filepath) not_sheet_get(filepath) # 定义一个函数,使参数内容可以实时更新在文本框内 def text_update(text_str, color="black"): textvar = str(text_str) bt7.insert('insert', time.strftime("%H:%M:%S") + ": " + textvar + '\n', color) bt7.update() def sheet_get(): """ 单选时处理文件数据 :return: """ value = bt4.get(bt4.curselection()) # bt4是一个列表选取框,这里获取选取的值 var2.set(value) text_update("你选择了Sheet:%s" % value) text_update("正在处理数据..") if filepath == "": text_update("文件路径出错", color="red") else: try: text = er.work_run(filepath, value) text_update(text, color="blue") except FileError as e: messge = e.msg text_update(str(messge), color="red") num_s = 0 num_f = 0 def not_sheet_get(file): global num_s global num_f """ 多选文件时处理数据 :param file: :return: """ text_update("%s正在处理数据.." % file) if file == "": text_update("导入excel文件有误", color="red") else: try: text = er.work_run(file, sheetname="Sheet1") num_s += 1 text_update(text+fc_num(), color="blue") except FileError as e: messge = e.msg num_f += 1 text_update(str(messge)+fc_num(), color="red") def fc_num(): global num_s global num_f return "(成功:{0} 失败:{1} 已完成: {2} /总任务:{3})".format(num_s, num_f, num_s + num_f, num_filepaths) a = False def on_closing(): """ 控制程序关闭 :return: """ global a if messagebox.askokcancel("Quit", "你要关闭程序吗"): # 点击窗口关闭时弹出一个交互框 window.destroy() a = True window = tk.Tk() window.title("excel数据统计") window.geometry("500x500") filepath = "" var1 = tk.StringVar() var2 = tk.StringVar() var3 = tk.StringVar() bt1 = tk.Label(window, text="导入xlsx文件,需要导入的文件必须关闭!\n导入一个文件时可自选需要处理的sheet,\n如果导入多个文件,则默认处理'Sheet1'" , fg="black", font=("Arial", 12), width=50, height=4) bt2 = tk.Button(window, text="导入excel文件", width=15, height=2, command=open_file) bt3 = tk.Label(window, text="选择要处理的sheet", fg="black", font=("Arial", 12), width=50, height=2) bt4 = tk.Listbox(window, listvariable=var1, height=5) bt5 = tk.Button(window, text="确认选择该参数名", command=sheet_get) bt6 = tk.Label(window, textvariable=var2, fg="black", font=("Arial", 12), width=50, height=2) bt7 = tk.Text(window, bg="white", fg="black") bt7.tag_config("red", backgroun="yellow", foreground="red") bt7.tag_config("blue", backgroun="yellow", foreground="blue") bt7.tag_config("black", foreground="black") bt1.pack() bt2.pack() bt3.pack() bt4.pack() bt5.pack() bt6.pack() bt7.pack() window.protocol("WM_DELETE_WINDOW", on_closing) while True: window.mainloop() if a: break