How To Let Your Main Window Appear after succesful login in Tkinter(PYTHON 3.6

前端 未结 1 1499
挽巷
挽巷 2021-01-25 08:12

This is ui which comes with default user name and password but after successful login the main UI needs to appear

Challenge

  1. when you correctly input the
1条回答
  •  遥遥无期
    2021-01-25 08:39

    The below code can be used for the desired effect and is commented to show what is happening each step of the way:

    from tkinter import * #Imports Tkinter
    import sys #Imports sys, used to end the program later
    
    root=Tk() #Declares root as the tkinter main window
    top = Toplevel() #Creates the toplevel window
    
    entry1 = Entry(top) #Username entry
    entry2 = Entry(top) #Password entry
    button1 = Button(top, text="Login", command=lambda:command1()) #Login button
    button2 = Button(top, text="Cancel", command=lambda:command2()) #Cancel button
    label1 = Label(root, text="This is your main window and you can input anything you want here")
    
    def command1():
        if entry1.get() == "user" and entry2.get() == "password": #Checks whether username and password are correct
            root.deiconify() #Unhides the root window
            top.destroy() #Removes the toplevel window
    
    def command2():
        top.destroy() #Removes the toplevel window
        root.destroy() #Removes the hidden root window
        sys.exit() #Ends the script
    
    
    entry1.pack() #These pack the elements, this includes the items for the main window
    entry2.pack()
    button1.pack()
    button2.pack()
    label1.pack()
    
    root.withdraw() #This hides the main window, it's still present it just can't be seen or interacted with
    root.mainloop() #Starts the event loop for the main window
    

    This makes use of the Toplevel widget in order to create a window which asks for the users details and then directs them to the main window which you can setup as you please.

    You are also still able to use the pop up messages you have used in your example and if required you can also change the size of the Toplevel widget.

    Please be advised however that this is not a particularly secure way of managing passwords and logins. As such I would suggest that you look up the proper etiquette for handling sensitive information in programming.

    0 讨论(0)
提交回复
热议问题