【问题标题】:Getting KeyError when calling self controller.method() in a tkinter app在 tkinter 应用程序中调用 self controller.method() 时出现 KeyError
【发布时间】:2019-08-01 19:24:16
【问题描述】:

我已经构建了一个多页 tkinter 应用程序。该应用程序的结构使得主 GUI 存储在一个 .py 文件中,而其他页面存储在主应用程序内的 dict 中。主应用程序在每个页面中被引用为self.controller

当我尝试使用控制器中的方法时,有时它会产生 KeyError,有时却不会,而且我终其一生都无法弄清楚原因。

我已经根据社区提供的答案重写了这组页面,每次我这样做都会解决一个问题,但它仍然没有解决我试图解决的问题(在页面之间传递信息”

下面的代码是我的主要应用程序(我稍后会将代码黑化)

import tkinter as tk
from front_page import FrontPage
from main_page import GamePage



class GUI(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)

        # Initialize Window
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)


        #Initialise each page
        self.frames = {}
        for F in (FrontPage, GamePage):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame


            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame("FrontPage")#This one works perfecly




    # shows the desired frame
    def show_frame(self, page_name):
        frame = self.frames[page_name]
        frame.tkraise()


    def get_page(self, page_name):#I think the problem is actually here
        return self.frames[page_name]

app = GUI()
app.geometry("400x600")
app.mainloop()

现在在 FrontPage 我使用按钮来配置 GamePage 上的标签 这就是我得到追溯的地方。对此的实现是:

class FrontPage(tk.Frame):
    """This is a class to make the front page
    it inherits from tk.Frame
    """

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller

        self.mainframe = tk.Frame(self)
        self.mainframe.pack(expand=True, fill=tk.BOTH)

        (...)

        gamepage = self.controller.get_page("GamePage")#this is the problem
        self.forward_label = gamepage.name_label
        #when I comment out these lines everything else works

    def on_button(self):
        self.output_frame.configure(text = self.name_entry.get())
        self.forward_label.configure(text = self.name_entry.get())

此代码产生回溯

Traceback(最近一次调用最后一次):

文件“/Users/kevinomalley/Desktop/python_work/rapid_rpg/app_GUI.py”,第50行,在

app = GUI()

文件“/Users/kevinomalley/Desktop/python_work/rapid_rpg/app_GUI.py”,第 22 行,在 init

frame = F(parent=container, controller=self)

文件“/Users/kevinomalley/Desktop/python_work/rapid_rpg/front_page.py”,第 106 行,在 init

gamepage = self.controller.get_page("GamePage")

文件“/Users/kevinomalley/Desktop/python_work/rapid_rpg/app_GUI.py”,第 40 行,在 get_page
返回 self.frames[page_name]

KeyError: '游戏页面'

我不知道为什么我可以毫无问题地调用 controller.show_frame('') 方法,但 controller.get_page('') 方法返回 keyerror。

我们将不胜感激。

【问题讨论】:

    标签: python tkinter


    【解决方案1】:

    问题是您试图在创建页面“GamePage”之前对其进行引用。那是因为您将代码放在FrontPage.__init__ 中。您不能在创建页面之前引用它。

    【讨论】:

    • 再次感谢布莱恩的回答。我已经移动了一点代码,我仍然没有得到我真正想要的功能(接受用户输入并将其传递给 GamePage 上的标签),但你的回答已经修复了追溯。
    猜你喜欢
    • 2012-05-30
    • 1970-01-01
    • 2020-11-21
    • 2015-11-22
    • 1970-01-01
    • 2017-11-17
    • 1970-01-01
    • 1970-01-01
    • 2021-02-18
    相关资源
    最近更新 更多