【问题标题】:Why am I getting this error with Python tkinter: KeyError: <class '__main__.AAA_Page'>?为什么我在 Python tkinter 中出现此错误:KeyError: <class '__main__.AAA_Page'>?
【发布时间】:2017-04-17 20:40:15
【问题描述】:

我在 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'(>)

我查看了我的代码,但我看不出哪里出错了。任何帮助将不胜感激,谢谢!

【问题讨论】:

    标签: python python-3.x lambda tkinter


    【解决方案1】:

    不要在类中的方法之外放置任何代码。所有这些按钮都需要再缩进一次,因此它们是 __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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-09
      • 2016-06-09
      • 2021-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-10
      相关资源
      最近更新 更多