2017-04-26 5 views
-2

아래 코드는 사용자가 해당 페이지로 이동할 수있는 일련의 페이지가 포함 된 화학 퀴즈 응용 프로그램을 보여줍니다. 어떤 이유 KeyError : <class '__main __. STAGE1'>

class STARTPAGE(tk.Frame): 
    def __init__(self, parent, controller): 
     tk.Frame.__init__(self, parent) 
     Instructions1 = tk.Label(self, text=("You will obtain a Desired Product that is chosen randomly from a Starting Reactant."))              
     Instructions2 = tk.Label(self, text=("You get 3 Attempts to complete the question in which Points will be added to your score ")) 
     Instructions3 = tk.Label(self, text=("After 3 attempts you will be moved onto next stage or question")) 
     Instructions4 = tk.Label(self, text=("You need to select one Reagent and 1 or 2 Conditions to complete stage")) 
     Instructions5 = tk.Label(self, text=("Your score will be ranked among others in your same class in a leaderboard")) 

     Instructions1.grid(row=0, column=0) 
     Instructions2.grid(row=1, column=0) 
     Instructions3.grid(row=2, column=0) 
     Instructions4.grid(row=3, column=0) 
     Instructions5.grid(row=4, column=0) 

     StartQuiz = tk.Button(self, text="Start Quiz", command=lambda: controller.show_frame(STAGE1)) 
     StartQuiz.grid(row=8, column=1) 

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

     Stage1 = tk.Label(self, text="STAGE 1") 
     Stage1.grid(column=1, row=0)   
     #Explain the Main class and tk frame constructor in each class from stack overflow   
     self.ButtonData = BUTTONDATA(self, tk.Frame) 

     self.ButtonData.ReagentOption1.config(command=lambda: self.AllocatePointsStage(1)) 
     self.ButtonData.ReagentOption2.config(command=lambda: self.AllocatePointsStage(1)) 
     self.ButtonData.ReagentOption3.config(command=lambda: self.AllocatePointsStage(1)) 
     self.ButtonData.ReagentOption4.config(command=lambda: self.AllocatePointsStage(1)) 

     self.ButtonData.ConditionOption1.config(command=lambda: self.AllocatePointsStage(1)) 
     self.ButtonData.ConditionOption2.config(command=lambda: self.AllocatePointsStage(1)) 
     self.ButtonData.ConditionOption3.config(command=lambda: self.AllocatePointsStage(1)) 
     self.ButtonData.ConditionOption4.config(command=lambda: self.AllocatePointsStage(1)) 

     self.ButtonData.ReagentOption1.grid(column=2, row=5) 
     self.ButtonData.ReagentOption2.grid(column=2, row=6) 
     self.ButtonData.ReagentOption3.grid(column=2, row=7) 
     self.ButtonData.ReagentOption4.grid(column=2, row=8) 

     self.ButtonData.ConditionOption1.grid(column=10, row=5) 
     self.ButtonData.ConditionOption2.grid(column=10, row=6) 
     self.ButtonData.ConditionOption3.grid(column=10, row=7) 
     self.ButtonData.ConditionOption4.grid(column=10, row=8) 

     self.Continue = tk.Button(self, text="Continue", command=lambda: controller.show_frame(Stage2)) 
     self.Continue.grid(column=6) 

     self.QuestionStarter = QUESTIONSTARTER(self, tk.Frame) 
     self.QuestionStarter.PointsLabel.grid(row=0, column=6) 
     self.QuestionStarter.AttemptsDisplayed.grid(row=1, column=7) 
     self.QuestionStarter.WordedQuestion.grid(row=0, column=1) 
     self.QuestionStarter.AttemptsDisplayedText.grid(row=1, column=8) 
     DesiredProductLabel = tk.Label(self, command=lambda: ShowDesiredProduct(DesiredProduct)) 
     DesiredProductLabel.grid(row=5, column=0) 

    def ShowDesiredProduct(self, DesiredProduct): 
     DesiredProduct = GetDesiredProduct() 
     return DesiredProduct 

class Main(tk.Tk): 
    def __init__(self, *args, **kwargs): 
     tk.Tk.__init__(self, *args, **kwargs) 
     tk.Tk.wm_title(self, "Chemistry Quiz") 
     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 = {} 

     for F in (STUDENTLOGINPAGE, STAFFLOGINPAGE, STARTPAGE, ASSIGNMENTPAGE, STAGE1, STAGE2, STAGE3, STAGE4, STAGE5, LEADERBOARDPAGE): 

      frame = F(container, self) 

      self.frames[F] = frame 

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

      self.show_frame(STUDENTLOGINPAGE) 
      if self.frames == []: 
       print ("Error in loading program") 
      #This class inherits from Tk class within module of Tk Inter 
      #This basically displays each page in turn from the List 

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

    def get_page(self, page_class): 
     return self.frames[page_class] 

, 내가 1 단계 내 쇼 프레임 내에서 발생하는 다음과 같은 오류를 얻을 :

Error -Traceback (most recent call last): 
    File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/tkinter/__init__.py", line 1549, in __call__ 
    return self.func(*args) 
    File "/Users/ammarhusain/Desktop/Computing Project/Ammar Chemistry Program FINAL.py", line 316, in <lambda> 
    StartQuiz = tk.Button(self, text = "Start Quiz", command=lambda: controller.show_frame(STAGE1)) 
    File "/Users/ammarhusain/Desktop/Computing Project/Ammar Chemistry Program FINAL.py", line 3152, in show_frame 
    frame = self.frames[cont] 
KeyError: <class '__main__.STAGE1'> 
+0

입니까? – VMAtm

+0

그것은 학생 로그인을위한 페이지 인 또 다른 클래스입니다. 공간을 차지할 수 없었습니다 @VMAtm – Ammar

+0

KeyError를 디버그하는 첫 번째 단계는 사전에서 키를 출력하여 오류가 발생한 코드 바로 앞에 출력하고, 사전에있는 키가 실제로 사전에 있는지 확인하십시오. 또한'cont'에 여러분이 생각하는 내용이 포함되어 있는지 확인해야합니다. –

답변

0

여기서 문제는 당신이 키와 클래스 정의를 사용하고 있다는 것입니다 :

:

# add the frame 
self.frames[F] = frame 
# show the frame 
self.show_frame(STUDENTLOGINPAGE) 

나는 사전에서 키로 문자열을 사용하고, 루프에서이 줄을 이동할 수 제안

controller.show_frame(STAGE1) 

그래서, 컨트롤러 버튼을 클릭 한 시간에 STAGE1 화면이 포함되어 있지 않습니다

지금 다시 스택 트레이스에, 당신이이 라인을 가지고있다. 당신이 STUDENTLOGINPAGE 후 모든 화면이 생성되고이 보일 경우, 문제는 외출해야 STUDENTLOGINPAGE은 무엇

# create all the screens 
for F in (STUDENTLOGINPAGE, STAFFLOGINPAGE, STARTPAGE, ASSIGNMENTPAGE, STAGE1, STAGE2, STAGE3, STAGE4, STAGE5, LEADERBOARDPAGE): 
    frame = F(container, self) 
    self.frames[F] = frame 
    frame.grid(row=0, column=0, sticky="nsew") 

# check the sreens availability 
if self.frames == []: 
    print ("Error in loading program") 

# now show the login form 
self.show_frame(STUDENTLOGINPAGE) 
+0

도움을 주셔서 감사합니다.하지만 여전히 이해하지 못합니다. 키를 문자열에 넣고 모든 화면이 끝난 후 STUDENTLOGINPAGE를 보여 주면 무슨 뜻 인지요? @VMAtm 또한 놀랄만한 내 프로그램의 다른 문제에 관해서도 연락 할 수 있습니다 !! – Ammar

+0

문자열 정보 - 사전 키를 클래스로 사용하는 것이 읽기 쉽지 않기 때문에 단지 조언 일뿐입니다. 'STUDENTLOGINPAGE'에 대해서 - 당신은 모든 화면을 생성하기 전에 당신의 루프 :'self.show_frame (STUDENTLOGINPAGE)'에 그것을 보여줍니다. 'for'에서 그것을 밖으로 이동하십시오. 답변을 업데이트했습니다. – VMAtm

+0

Ahh ok 알았지 만 지금 프로그램을 실행하면 StudentLogin 페이지가 아닌 Assignment Page 화면이 나타납니다. 그리고 컨트롤러를 사용하여 할당 페이지에서 1 단계로 이동하면 동일한 오류가 발생합니다. 어쨌든 연락 드릴 수 있습니까? 매우 유용합니다. @ VMAtm – Ammar