python - 使用函数切换帧
问题描述
我有一个执行帧切换的程序,如下例所示: Switch between two frames in tkinter
我试图通过一个函数使我的程序的一部分切换帧,其中切换帧的命令位于另一个函数内部,而不是按钮。
这是切换帧功能:
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])
除了 switch_frame 行之外,这个函数的所有其他东西都可以工作。
这是使用此功能的框架之一:
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)
当我单击帧 q1 上使用“validateAns”的按钮时,我收到错误消息“名称'self'未定义”。我尝试用“master.switch_frame”替换 self.switch_frame 并得到类似的错误,但没有定义“master”。
根据传递给 validateAns 函数的值,页面/框架应该切换到三个不同的之一。
解决方案
推荐阅读
- delphi - 如何配置 Delphi RAD Server 以接受承载授权方案?
- java - 即使编译了 jvm,什么样的测试用例也会使我的 java 程序崩溃?
- flutter - Flutter,自定义“表单包装器”
- python - 如何改进深度学习回归模型的预测
- c# - 2.5d瞄准在右方向而不是左方向
- perl - Hashref 分配消耗下一个键而不是分配 undef
- indentation - 如何在 Vivado 中启用缩进指南?
- javascript - TypeError:无法读取未定义的属性“getElementById”
- reactjs - 当后退按钮导航在 Safari 或 Firefox 上不起作用时滚动到顶部
- redhat - RHEL 7.9 Maipo 中没有可用的软件包 llvm-toolset-7.0