我想知道为什么我无法在Tkinter类中创建excel工作簿?
我在常规脚本中编写完全相同的代码,但每当我将代码放入带有定义的更大脚本并希望将其添加到tkinter类中时,就不会创建excel工作簿。有谁知道如何解决这个问题?
代码:
from openpyxl import Workbook
wb =Workbook(r"C:\Myfile.xlsx")
ws1 = wb.active
ws1.title = "First"
ws2 = wb.create_sheet("Second")
ws3 = wb.create_sheet("Third")
ws4 = wb.create_sheet("Fourth")
ws5 = wb.create_sheet("Fifth")
wb.save(r"C:\Myfile.xlsx")
当我单独运行这个excel文件的时候,但是当我把它放到一个类中时它没有。
from openpyxl import Workbook
import os
from tkinter import *
from tkinter import messagebox
LARGE_FONT = ("Verdana", 12)
NORM_FONT = ("Verdana", 10)
SMALL_FONT = ("Verdana", 8)
current_dir = os.getcwd()
class Gui(Tk):
def __init__(self, *args, **kwargs):
Tk.__init__(self, *args, **kwargs)
Tk.wm_title(self, "Program")
container = Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.geometry("350x500")
self.frames = {}
frame = StartPage(container, self)
self.frames[StartPage] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
label = Label(self, text="Program", font=LARGE_FONT)
label.pack(pady=10, padx=10)
self.buttons()
self.program = ""
def runanalyze0(self, *args):
try:
wb =Workbook(r"C:\Myfile.xlsx")
ws1 = wb.active
ws1.title = "First"
ws2 = wb.create_sheet("Second")
ws3 = wb.create_sheet("Third")
ws4 = wb.create_sheet("Fourth")
ws5 = wb.create_sheet("Fifth")
wb.save(r"C:\Myfile.xlsx")
finally:
self.finishpop()
def finishpop(msg):
messagebox.showinfo("Done","Done")
def buttons(self):
self.pack(fill=BOTH, expand=1)
button = Button(self, text="Xlsx file", command=self.runanalyze0())
button.place(x=50, y=150)
e1 = Entry(self, bd=5)
e1.pack(side=BOTTOM)
e1.insert(0, "{}".format(current_dir))
if __name__ == "__main__":
app = Gui()
app.mainloop()
Traceback:
FileNotFoundError: [Errno 2] No such file or directory:
'C:\\Myfile.xlsx'
使用Python 3.4.1。谢谢你的帮助!