如何调用我用语句初始化的另一个帧?

时间:2015-02-18 10:21:54

标签: python tkinter

如何调用我用声明初始化的另一帧?

这是我的代码:

class MindQuiz(tk.Tk):

    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)
        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 (LogIn, Register, PageTwo):

            frame = F(container, self)

            self.frames[F] = frame

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

        self.show_frame(LogIn)

    def show_frame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()


class LogIn(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)
        self.errorcount=0

        self.usernameEntry = tk.Entry(self,width=20,font=('Segoe Ui',15))
        self.usernameEntry.place(x=200,y=300)
        self.passwordEntry = tk.Entry(self,width=20,font=('Segoe Ui',15),show='*')
        self.passwordEntry.place(x=200,y=400)

        button = ttk.Button(self, text="Visit Page 1",
                            command=lambda: controller.show_frame(Register))
        button.place(x=100,y=100)

        button2 = ttk.Button(self, text="Visit Page 2",
                            command=lambda: controller.show_frame(PageTwo))
        button2.place(x=150,y=150)

        submit = ttk.Button(self, text="Log In",width=10,
                            command= self.CheckAccount)
        submit.place(x=350,y=150)

    def CheckAccount(self):
        f= open('account.txt','r+')
        found = 0
        username = self.usernameEntry.get()
        password = self.passwordEntry.get()
        for line in f:
            str=line.split("~")
            "*".join(str)
            if(username==str[0] and password==str[1]):
                    tk.messagebox.showinfo(title="Success", message="Welcome!\n Successfully Logged In!" )
                    found=1;
            if(found==1):
                break;
        if(found==0):
            self.errorcount +=1;
            tk.messagebox.showinfo(title="Error", message="Invalid!\n Incorrect Username Or Password!")
        if(self.errorcount>=3):
            sys.exit()
        print(self.errorcount);
        if(found==1):
            self.controller.show_frame(Register) ##<-------------------

1 个答案:

答案 0 :(得分:1)

您需要在controller中初始化__init__,如下所示:

class LogIn(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)
        self.controller = controller
        self.errorcount=0

现在您的代码可以使用。