Skip to content Skip to sidebar Skip to footer

Two Windows: First Login After That Main Program

i have this program class loginWindow(): def __init__(self, master): self.master = master self.frame = Frame(master) master.title(u'Geometry Calc - Log

Solution 1:

You can use Toplevel for login window and withdraw , deiconify methods to hide and show root window respectively.

Check the following code:

from Tkinter import *


classloginWindow(Toplevel):
    def__init__(self, title, parent):
        Toplevel.__init__(self, parent)
        # Save parent reference to use it 
        self.parent = parent
        self.parent.title(u"Geometry Calc - Login")
        Button(self, text="Login", command=self.login).pack()

    deflogin(self):

        access =  True# Used to test if a user can login.if access:
            # Close Toplevel window and show root window
            self.destroy()
            self.parent.deiconify()
        else:
            self.parent.quit()



classmain(Tk):
    def__init__(self, *args, **kwargs):
        Tk.__init__(self, *args, **kwargs)
        self.title(u"Geometry Calc")  # Nadpis
        self.geometry("695x935")  # Rozmery v px
        self.config(background="white")
        self.resizable(width=FALSE, height=FALSE)  # Zakážeme změnu rozměrů uživatelem - zatím..

        menubar = Menu(self)

        helpmenu = Menu(menubar, tearoff=0)
        helpmenu.add_command(label="Konec", command=self.quit)
        menubar.add_cascade(label="Soubor", menu=helpmenu)
        helpmenu = Menu(menubar, tearoff=0)
        helpmenu.add_command(label="O programu", command=self.createAbout)
        menubar.add_cascade(label="Pomoc", menu=helpmenu)
        self.config(menu=menubar)

        canvas = Canvas(self, width=691, height=900)
        canvas.pack(expand=1, fill=BOTH)

        # Hide root window
        self.withdraw()

        # Lunch login window
        loginWindow('Frame', self)


    defcreateAbout(self):
        passdefquit(self):
        self.destroy()



app = main()

app.mainloop()

If youn want to use 2 Toplevel windows for login and main app, root window should be hidden:

classloginWindow(Toplevel):def__init__(self, title, parent):
        Toplevel.__init__(self, parent)
        ...

    deflogin(self):
       ifaccess:# Close Toplevel window and lunch root windowself.destroy()
            main()



classmain(Toplevel):def__init__(self, *args, **kwargs):
        Toplevel.__init__(self, *args, **kwargs)
        ...



root = Tk()
root.withdraw()

loginWindow('title', root)

root.mainloop()  

Post a Comment for "Two Windows: First Login After That Main Program"