为什么我在Python tkinter中出现此错误:KeyError:<class'__main __。aaa_page'=“”>?

时间:2017-04-17 20:40:16

标签: python python-3.x lambda tkinter

我在tkinter比较新。我的项目是利用tkinter GUI并制作一个三角形解算器,它将解决三角形环境下的所有边和角度。这是我的代码:

import tkinter as tk
LARGE_FONT = ("Verdana", 14)
SMALL_FONT = ("Verdana", 8)
class CreatePT(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 = {}
    #This for loop allows the program to access the necessary page
    for F in (WelcomePage, AAA_Page):
        frame = F(container,self)

        self.frames[F] = frame

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

    self.show_frame(WelcomePage)

def show_frame(self,cont):
    frame = self.frames[cont]
    frame.tkraise()

class WelcomePage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)
        label = tk.Label(self, text = "Welcome to the Triangle Solver",font=LARGE_FONT)
        label.pack(pady = 10, padx = 10)

    AAA_button = tk.Button(self, text = "AAA(Angle-Angle-Angle",
        command = lambda: controller.show_frame(AAA_Page))
    AAA_button.pack()

    AAS_button = tk.Button(self, text = "AAS(Angle-Angle-Side)",
        command = lambda: controller.show_frame(AAS_Page))
    AAS_button.pack()

    ASA_button = tk.Button(self, text = "ASA(Angle-Angle-Side)",
        command = lambda: controller.show_frame(ASA_Page))
    ASA_button.pack()

    SSA_button = tk.Button(self, text = "SSA(Side-Side-Angle)",
        command = lambda: controller.show_frame(SSA_Page))
    SSA_button.pack()

    SAS_button = tk.Button(self, text = "SAS(Side-Angle-Side)",
        command = lambda: controller.show_frame(SAS_Page))
    SAS_button.pack()

    SSS_button = tk.Button(self, text = "SSS(Side-Side-Side)",
        command = lambda: controller.show_frame(SSS_Page))
    SSS_button.pack()
class AAA_Page(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)
        label = tk.Label(self, text = "AAA(Angle-Angle-Angle)",font=LARGE_FONT)
        label.pack(pady = 10, padx = 10)

    button1 = tk.Button(self, text = "Back to Home",
        command = lambda: controller.show_frame(WelcomePage))
    button1.pack()
    warning_label = tk.Label(self, text = "Due to the given measurements, the program can only solve for the remaining angle", font = SMALL_FONT)#possible to make red?
    warning_label.pack()
    angle1_label = tk.Label(self, text = "Angle 1 =")
    angle1_label.pack(side = right)
    angle1_entry = tk.Entry(self, bd = 5)
    angle1_entry.pack(side = left)
class AAS_Page(tk.Frame):#NOTDONE
    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)
        label = tk.Label(self, text = "AAS(Angle-Angle-Side)",font=LARGE_FONT)
        label.pack(pady = 10, padx = 10)

    button1 = tk.Button(self, text = "Back to Home",
        command = lambda: controller.show_frame(WelcomePage))
    button1.pack()
#Regular Page

class ASA_Page(tk.Frame):#NOTDONE
    #Intialization method
    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)
        label = tk.Label(self, text = "ASA(Angle-Side-Angle)",font=LARGE_FONT)
        label.pack(pady = 10, padx = 10)

    button1 = tk.Button(self, text = "Back to Home",
        command = lambda: controller.show_frame(WelcomePage))
    button1.pack()
#Regular Page

class SSA_Page(tk.Frame):#NOTDONE
    #Intialization method
    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)
        label = tk.Label(self, text = "SSA(Side-Side-Angle)",font=LARGE_FONT)
        label.pack(pady = 10, padx = 10)

    button1 = tk.Button(self, text = "Back to Home",
        command = lambda: controller.show_frame(WelcomePage))
    button1.pack()
#Regular Page

class SAS_Page(tk.Frame):#NOTDONE
    #Intialization method
    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)
        label = tk.Label(self, text = "AAA(Side-Angle-Side)",font=LARGE_FONT)
        label.pack(pady = 10, padx = 10)

    button1 = tk.Button(self, text = "Back to Home",
        command = lambda: controller.show_frame(WelcomePage))
    button1.pack()
#Regular Page

class SSS_Page(tk.Frame):#NOTDONE
    #Intialization method
    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)
        label = tk.Label(self, text = "SSS(Side-Side-Side)",font=LARGE_FONT)
        label.pack(pady = 10, padx = 10)

    button1 = tk.Button(self, text = "Back to Home",
        command = lambda: controller.show_frame(WelcomePage))
    button1.pack()

create = CreatePT()
create.mainloop()

GUI出来很好,但每当我尝试切换到AAA_Page框架进行测试时,我都会收到以下错误:

Traceback (most recent call last):
File "C:\Users\bamak\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 1699, in __call__
   return self.func(*args)
File "C:\Users\bamak\Desktop\CodingProjects\Triangle Solver--Create PT.py", line 49, in <lambda>
   command = lambda: controller.show_frame(AAA_Page))
File "C:\Users\bamak\Desktop\CodingProjects\Triangle Solver--Create PT.py", line 37, in show_frame
   frame = self.frames[cont]
KeyError:(<)class '__main__.AAA_Page'(>)

我查看了我的代码,但我不知道我哪里出错了。非常感谢任何帮助,谢谢!

1 个答案:

答案 0 :(得分:0)

不要在类中放置方法之外的任何代码。所有这些按钮需要再缩进一次,因此它们是__init__方法的一部分,如下所示:

class WelcomePage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)
        label = tk.Label(self, text = "Welcome to the Triangle Solver",font=LARGE_FONT)
        label.pack(pady = 10, padx = 10)

        AAA_button = tk.Button(self, text = "AAA(Angle-Angle-Angle",
            command = lambda: controller.show_frame(AAA_Page))
        AAA_button.pack()