下面的代码显示了一个化学测验应用程序,其中包含一系列页面,用户可以在这些页面之间切换。
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 :(得分:0)
这里的问题是您使用类定义作为键:
# add the frame
self.frames[F] = frame
# show the frame
self.show_frame(STUDENTLOGINPAGE)
我建议你在字典中使用字符串作为键,并将这些行移出循环:
self.show_frame(STUDENTLOGINPAGE)
现在回到你的堆栈跟踪,你有这条线:
controller.show_frame(STAGE1)
因此,控制器在点击按钮时不会包含STAGE1
屏幕。如果您在后显示所有正在创建的屏幕后显示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)