Tkinter 在尝试更改帧时抛出 KeyError [英] Tkinter throwing a KeyError when trying to change frames

查看:23
本文介绍了Tkinter 在尝试更改帧时抛出 KeyError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从 Sentdex 教程中学习 tkinter,但在尝试更改页面时遇到了问题.我的编译器会抛出一些关于 KeyError 的信息,每当我将起始页上的按钮更改为自身而不是 PageOne 时,它​​都不会给出该错误:

I'm learning tkinter off of the Sentdex tutorials and I into a problem when trying to change pages. My compiler throws something about a KeyError that it doesn't give whenever I change the button on the start page to change to itself rather than PageOne:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Jason\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 1549, in __call__
    return self.func(*args)
  File "C:/Users/Jason/PycharmProjects/gui/main.py", line 43, in <lambda>
    button1 = tk.Button(self, text="Visit Page 1",command=lambda: controller.show_frame(PageOne))
  File "C:/Users/Jason/PycharmProjects/gui/main.py", line 29, in show_frame
    frame = self.frames[cont]
KeyError: <class '__main__.PageOne'>

和我的代码:

import tkinter as tk

LARGE_FONT=("Verdana", 12)


class SeaofBTCapp(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()

def qf(param):
    print(param)

class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Start Page", font=LARGE_FONT)
        label.pack(pady=10,padx=10)

        #command within button cant throw args to funcs. Use lambda to throw those args to the func instead
        button1 = tk.Button(self, text="Visit Page 1",command=lambda: controller.show_frame(PageOne))
        button1.pack()

class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Start Page", font=LARGE_FONT)
        label.pack(pady=10,padx=10)

        #command within button cant throw args to funcs. Use lambda to throw those args to the func instead
        button1 = tk.Button(self, text="Visit Page 1",command=lambda: controller.show_frame(StartPage))
        button1.pack()


app = SeaofBTCapp()
app.mainloop()

推荐答案

您的按钮正在调用 show_frame(PageOne),但您从未创建过 PageOne 的实例.所以,当然,self.frames 中没有匹配该页面的键.

Your button is calling show_frame(PageOne), but you never created an instance of PageOne. So, of course, there is no key in self.frames that matches that page.

也许您打算在初始代码中创建 PageOne 的实例?

Perhaps you intended to create an instance of PageOne in your initial code?

def __init__(self, *args, **kwargs):
    ...
    frame = PageOne(container, self)
    self.frames[PageOne] = frame
    ...

如果您在需要之前不想创建页面,您可以在 show_frame 中添加该代码.首先,您需要使 container 成为一个实例变量(即:self.container),然后将 show_frame 修改为如下所示:

If you don't want to create the page until you need it, you can add that code in show_frame. First, you'll need to make container an instance variable (ie: self.container), then modify show_frame to look something like this:

def show_frame(self, cont):
    if cont not in self.frames:
        self.frames[cont] = cont(self.container, self)
    frame = self.frames[cont]
    frame.tkraise()

这篇关于Tkinter 在尝试更改帧时抛出 KeyError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆