python - 我收到 KeyError:
我收到此错误 -
KeyError: <class '__main__.PublishPage'>
这是我的代码 -
p = 0
#N
问题描述
我收到此错误 -
KeyError: <class '__main__.PublishPage'>
这是我的代码 -
p = 0
#Now, Adding the different pages for the game
class HeadlineGame(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, controller):
frame = self.frames[controller]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
publishbutton = tk.Button(self, image=publishimg, borderwidth=0, command= lambda: controller.show_frame(PublishPage))
publishbutton.image = publishimg
publishbutton.place(x=503, y=315)
class PublishPage(tk.Frame):
def __init__(self, parent, controller):
button2 = tk.Button(self, text="Test")
button2.grid(row=0, column=0)
app = HeadlineGame()
app.geometry('1366x768')
app.mainloop()
我试图解决这个问题,但无济于事。这是 lambda 命令的问题吗?我想要它,以便当我按下按钮时,它会将我带到第二帧,即使这个特定问题无法解决,有没有办法实现这一点?
谢谢你。
编辑回溯错误:
> Exception in Tkinter callback Traceback (most recent call last): File
> "C:\Users\DELL\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py",
> line 1883, in __call__
> return self.func(*args) File "C:\Users\DELL\Desktop\Python\intro.py", line 276, in <lambda>
> publishbutton = tk.Button(self, image=publishimg, borderwidth=0, command= lambda: controller.show_frame(PublishPage)) File
> "C:\Users\DELL\Desktop\Python\intro.py", line 197, in show_frame
> frame = self.frames[controller] KeyError: <class '__main__.PublishPage'>
我建议重构这样的东西。show_frame()
现在将负责根据请求实例化一个新框架;你不必管理self.frames
自己。
class HeadlineGame(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.container = 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 = {}
self.show_frame(StartPage)
def show_frame(self, controller):
if controller not in self.frames:
self.frames[controller] = frame = controller(self.container, self)
frame.grid(row=0, column=0, sticky="nsew")
frame = self.frames[controller]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
publishbutton = tk.Button(
self, image=publishimg, borderwidth=0, command=lambda: controller.show_frame(PublishPage)
)
publishbutton.image = publishimg
publishbutton.place(x=503, y=315)
class PublishPage(tk.Frame):
def __init__(self, parent, controller):
button2 = tk.Button(self, text="Test")
button2.grid(row=0, column=0)
app = HeadlineGame()
app.geometry("1366x768")
app.mainloop()
解决方案
我建议重构这样的东西。show_frame()
现在将负责根据请求实例化一个新框架;你不必管理self.frames
自己。
class HeadlineGame(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.container = 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 = {}
self.show_frame(StartPage)
def show_frame(self, controller):
if controller not in self.frames:
self.frames[controller] = frame = controller(self.container, self)
frame.grid(row=0, column=0, sticky="nsew")
frame = self.frames[controller]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
publishbutton = tk.Button(
self, image=publishimg, borderwidth=0, command=lambda: controller.show_frame(PublishPage)
)
publishbutton.image = publishimg
publishbutton.place(x=503, y=315)
class PublishPage(tk.Frame):
def __init__(self, parent, controller):
button2 = tk.Button(self, text="Test")
button2.grid(row=0, column=0)
app = HeadlineGame()
app.geometry("1366x768")
app.mainloop()
推荐阅读
- kotlin - 带有 Android Room TypeConverters 的 Kotlin 类型别名
- android-studio - Android studio 配置命令行属性
- command-line - 我们如何使用命令行在 Doxygen 中使用我的项目特定参数编辑生成的配置文件?
- django - 从Django视图传递参数到表单不起作用
- html - 我希望图标更进一步和平衡
- ios - react-native-firebase 在通知打开时没有注册的监听器 ios
- python - 如何使用贝叶斯证据来比较模型
- html - HTML 视频标签是否适用于 grails?
- css - CSS 直到手动刷新后才加载
- automationanywhere - 在 Automation Anywhere 中从 Excel(逐行)读取数据到 Web?
我收到此错误 -
KeyError: <class '__main__.PublishPage'>
这是我的代码 -
p = 0
#N
问题描述
我收到此错误 -
KeyError: <class '__main__.PublishPage'>
这是我的代码 -
p = 0
#Now, Adding the different pages for the game
class HeadlineGame(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, controller):
frame = self.frames[controller]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
publishbutton = tk.Button(self, image=publishimg, borderwidth=0, command= lambda: controller.show_frame(PublishPage))
publishbutton.image = publishimg
publishbutton.place(x=503, y=315)
class PublishPage(tk.Frame):
def __init__(self, parent, controller):
button2 = tk.Button(self, text="Test")
button2.grid(row=0, column=0)
app = HeadlineGame()
app.geometry('1366x768')
app.mainloop()
我试图解决这个问题,但无济于事。这是 lambda 命令的问题吗?我想要它,以便当我按下按钮时,它会将我带到第二帧,即使这个特定问题无法解决,有没有办法实现这一点?
谢谢你。
编辑回溯错误:
> Exception in Tkinter callback Traceback (most recent call last): File
> "C:\Users\DELL\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py",
> line 1883, in __call__
> return self.func(*args) File "C:\Users\DELL\Desktop\Python\intro.py", line 276, in <lambda>
> publishbutton = tk.Button(self, image=publishimg, borderwidth=0, command= lambda: controller.show_frame(PublishPage)) File
> "C:\Users\DELL\Desktop\Python\intro.py", line 197, in show_frame
> frame = self.frames[controller] KeyError: <class '__main__.PublishPage'>
我建议重构这样的东西。show_frame()
现在将负责根据请求实例化一个新框架;你不必管理self.frames
自己。
class HeadlineGame(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.container = 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 = {}
self.show_frame(StartPage)
def show_frame(self, controller):
if controller not in self.frames:
self.frames[controller] = frame = controller(self.container, self)
frame.grid(row=0, column=0, sticky="nsew")
frame = self.frames[controller]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
publishbutton = tk.Button(
self, image=publishimg, borderwidth=0, command=lambda: controller.show_frame(PublishPage)
)
publishbutton.image = publishimg
publishbutton.place(x=503, y=315)
class PublishPage(tk.Frame):
def __init__(self, parent, controller):
button2 = tk.Button(self, text="Test")
button2.grid(row=0, column=0)
app = HeadlineGame()
app.geometry("1366x768")
app.mainloop()
解决方案
我建议重构这样的东西。show_frame()
现在将负责根据请求实例化一个新框架;你不必管理self.frames
自己。
class HeadlineGame(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.container = 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 = {}
self.show_frame(StartPage)
def show_frame(self, controller):
if controller not in self.frames:
self.frames[controller] = frame = controller(self.container, self)
frame.grid(row=0, column=0, sticky="nsew")
frame = self.frames[controller]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
publishbutton = tk.Button(
self, image=publishimg, borderwidth=0, command=lambda: controller.show_frame(PublishPage)
)
publishbutton.image = publishimg
publishbutton.place(x=503, y=315)
class PublishPage(tk.Frame):
def __init__(self, parent, controller):
button2 = tk.Button(self, text="Test")
button2.grid(row=0, column=0)
app = HeadlineGame()
app.geometry("1366x768")
app.mainloop()
推荐阅读
- kotlin - 带有 Android Room TypeConverters 的 Kotlin 类型别名
- android-studio - Android studio 配置命令行属性
- command-line - 我们如何使用命令行在 Doxygen 中使用我的项目特定参数编辑生成的配置文件?
- django - 从Django视图传递参数到表单不起作用
- html - 我希望图标更进一步和平衡
- ios - react-native-firebase 在通知打开时没有注册的监听器 ios
- python - 如何使用贝叶斯证据来比较模型
- html - HTML 视频标签是否适用于 grails?
- css - CSS 直到手动刷新后才加载
- automationanywhere - 在 Automation Anywhere 中从 Excel(逐行)读取数据到 Web?