1
私は比較的新しいtkinterです。私のプロジェクトは、tkinterのGUIを利用して、三角形の状況下ですべての辺と角度を解決する三角ソルバーを作ることです。ここに私のコードは次のとおりです。私は自分のコード上で見てきましたPythonでこのエラーが発生する理由tkinter:KeyError:<class '__main __。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'(>)
:
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フレームに切り替えてみてくださいたびに、私は次のエラーを取得しますしかし、私はどこに間違っているのか分かりません。どんな助けでも大変感謝します。ありがとう!
これは機能しました。ありがとうございました! – Allstar