我有一个执行帧切换的程序,如本例所示:Switch between two frames in tkinter
我试图通过一个函数使程序的一部分切换帧,其中切换帧的命令在另一个函数中,而不是按钮
这是switch frame函数:
class MathsApp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self._frame=None
self.switch_frame(StartPage)
self.title("Maths Revision App")
self.geometry("800x500")
self.configure(bg="white")
def switch_frame(self, frame_class):
#Destroys current frame and replaces it with a new one.
new_frame=frame_class(self)
if self._frame is not None:
self._frame.destroy()
self._frame=new_frame
self._frame.pack(fill="both",expand=True)
这是我尝试切换帧的函数:
def validateAns(ans):
global questionpages,currentQ
if ans=="":
self.switch_frame(errorPage)
else:
if currentQ==9:
checkAns(ans, currentQ)
getTimes()
self.switch_frame(ResultPage)
else:
checkAns(ans, currentQ)
getTimes()
currentQ=currentQ+1
self.switch_frame(questionpages[currentQ])
除开关和框架线外,其他所有具有此功能的功能都正常工作
这是使用此函数的帧之一:
class q1(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master, bg="white")
lbl=tk.Label(self, text="Question 1", font=title_font, bg="white", fg="#004d99")
lbl.place(x=30, y=20)
txt=tk.Text(self, height=7, width=70)#the text box that displays the question
txt.config(state="normal")
txt.insert(tk.INSERT,qlist[0][0])
txt.config(state="disabled")
txt.place(x=35,y=125)
ans=tk.Entry(self)
ans.place(x=650, y=350,height=25)
btn=tk.Button(self, text="Next", height=3, width=15, fg="white", bg="#004d99", command=lambda:validateAns(ans.get()))
btn.place(x=650, y=400)
当我单击第1帧上使用“validateAns”的按钮时,我得到错误消息“name'self'not defined”。我尝试用“master.switch\u frame”替换self.switch\u frame,并得到一个类似的错误,但是没有定义“master”
根据传递给validateAns函数的值,页/帧应该切换到三个不同的值之一
目前没有回答
相关问题 更多 >
编程相关推荐