Python无法破坏登录框架

时间:2016-01-26 19:56:42

标签: python tkinter python-2.x

我正在创建一个登录和注册窗口。我想在点击注册按钮后销毁登录窗口框架,但我没有做到,有人可以帮助我吗?



from Tkinter import *
import tkMessageBox as tm

class Login:
    def __init__(self):
        login_window=Tk()
        login_window.title('PROJECTBOOK')

        self.usernameVar=StringVar()
        self.passwordVar=StringVar()
        
        frame1=Frame(login_window)
        frame1.pack()
        Label(frame1,text='USERNAME:').grid(row=1,sticky=W)
        self.entry1=Entry(frame1,textvariable=self.usernameVar,width=30)
        self.entry1.grid(row=1,column=1)

        frame2=Frame(login_window)
        frame2.pack()
        Label(frame2,text='PASSWORD:').grid(row=2,sticky=W)
        self.entry2=Entry(frame2,textvariable=self.passwordVar,width=30)
        self.entry2.grid(row=2,column=1)
        
        frame3=Frame(login_window)
        frame3.pack()
        self.Login_Btn=Button(frame3,text='LOGIN',fg='blue',command=self.login_btn).grid(row=1,column=1)
        self.Register_Btn=Button(frame3,text='REGISTER',fg='blue',command=self.register_btn).grid(row=1,column=2)

        login_window.mainloop()
        login_window.destroy()

    def login_btn(self):
        username=self.entry1.get()
        password=self.entry2.get()
        if not username:
            tm.showinfo('PROJECTBOOK','USERNAME MISSING!')
        elif not password:
            tm.showinfo('PROJECTBOOK','PASSWORD MISSING!')

    def register_btn(self):
        Register()

class Register:
    def __init__(self):
        register_window=Tk()
        register_window.title('REGISTER')

        self.usernameVar=StringVar()
        self.passwordVar=StringVar()
        self.confirmpasswordVar=StringVar()

        frame1=Frame(register_window)
        frame1.pack()
        Label(frame1,text='USERNAME:').grid(row=1,sticky=W)
        self.entry1=Entry(frame1,textvariable=self.usernameVar,width=30)
        self.entry1.grid(row=1,column=1)

        frame2=Frame(register_window)
        frame2.pack()
        Label(frame2,text='PASSWORD:').grid(row=2,sticky=W)
        self.entry2=Entry(frame2,textvariable=self.passwordVar,width=30)
        self.entry2.grid(row=2,column=1)

        frame3=Frame(register_window)
        frame3.pack()
        Label(frame3,text='CONFIRM PASSWORD:').grid(row=3,sticky=W)
        self.entry3=Entry(frame3,textvariable=self.confirmpasswordVar,width=20)
        self.entry3.grid(row=3,column=1)

        frame4=Frame(register_window)
        frame4.pack()
        self.Submit_Btn=Button(frame4,text='SUBMIT',fg='blue').grid(row=1,column=1)
        self.Back_Btn=Button(frame4,text='BACK',fg='red',command=register_window.destroy).grid(row=1,column=2)

        register_window.mainloop()

Login()




1 个答案:

答案 0 :(得分:1)

只需从destroy调用register_btn方法即可。为此,您需要将登录窗口添加到self

from Tkinter import *
import tkMessageBox as tm

class Login:
    def __init__(self):
        self.login_window=Tk()                    # <----
        self.login_window.title('PROJECTBOOK')    # <----

        self.usernameVar=StringVar()
        self.passwordVar=StringVar()

        frame1=Frame(self.login_window)           # <----
        frame1.pack()
        Label(frame1,text='USERNAME:').grid(row=1,sticky=W)
        self.entry1=Entry(frame1,textvariable=self.usernameVar,width=30)
        self.entry1.grid(row=1,column=1)

        frame2=Frame(self.login_window)    # <----
        frame2.pack()
        Label(frame2,text='PASSWORD:').grid(row=2,sticky=W)
        self.entry2=Entry(frame2,textvariable=self.passwordVar,width=30)
        self.entry2.grid(row=2,column=1)

        frame3=Frame(self.login_window)    # <----
        frame3.pack()

        self.Login_Btn=Button(frame3,text='LOGIN',fg='blue',command=self.login_btn).grid(row=1,column=1)
        self.Register_Btn=Button(frame3,text='REGISTER',fg='blue',command=self.register_btn).grid(row=1,column=2)

        self.login_window.mainloop()    # <----
        # self.login_window.destroy()   # <---- (not required)

    def login_btn(self):
        username=self.entry1.get()
        password=self.entry2.get()
        if not username:
            tm.showinfo('PROJECTBOOK','USERNAME MISSING!')
        elif not password:
            tm.showinfo('PROJECTBOOK','PASSWORD MISSING!')

    def register_btn(self):
        self.login_window.destroy()    # <----
        Register()


# Register class is unchanged

我发现你在这里使用类,在构造函数中完成所有处理,很奇怪。我想你有充分的理由这样做,但我会使用一些普通的功能。