使用功能切换框架

时间:2019-03-28 22:38:35

标签: python tkinter

我有一个执行帧切换的程序,例如以下示例: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”的按钮时,出现错误消息“未定义名称'自我'”。我尝试用“ master.switch_frame”替换self.switch_frame并得到类似的错误,但未定义“ master”。

取决于传递给validateAns函数的值,页面/框架应切换为三个不同的页面之一。

0 个答案:

没有答案
相关问题