跟着YouTube视频一起,但是我一直收到这个tkinter错误,而且我不知道它的起源是最模糊的线索
代码:
import tkinter as tk
G_FONT=("Verdana", 12)
class CapApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
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(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent, controller)
label = tk.Label(self, text="Start Page", font=G_FONT)
label.pack(pady=10, padx=10)
if __name__ == "__main__":
app = CapApp()
app.mainloop()
答案 0 :(得分:0)
同意错误消息令人困惑。无论如何,您的代码中有几个错误-已修复,并在以下代码的注释中指出:
import tkinter as tk
G_FONT=("Verdana", 12)
class CapApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
frame = StartPage(container, self)
self.frames[StartPage] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
# Fixed indentation so function is a class method.
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
# You're passing too many arguments the base class __init__()
# tk.Frame.__init__(self, parent, controller)1
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Start Page", font=G_FONT)
label.pack(pady=10, padx=10)
if __name__ == "__main__":
app = CapApp()
app.mainloop()