
In this post I am showing how to create an authentication box in Tkinter that has some basic functionality you would expect out of an authentication box. I just wanted this to be a simplistic design that was able to gather a users password and call another function, passing the username and password variable to that other function. I wanted it to have 3 features, a user name box, a password box that only displayed asterisks, and a login button that allowed us to move onto the next part of our program. Below is a video walk through of writing this code and the code itself.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
#!/usr/bin/python ''' Author: Kyle Kowalczyk Description: Username and password entry box Python Interpreter: v2.7.13 ''' from Tkinter import * # creates the main window object, defines its name, and default size main = Tk() main.title('Authentication Box') main.geometry('225x150') def clear_widget(event): # will clear out any entry boxes defined below when the user shifts # focus to the widgets defined below if username_box == main.focus_get() and username_box.get() == 'Enter Username': username_box.delete(0, END) elif password_box == password_box.focus_get() and password_box.get() == ' ': password_box.delete(0, END) def repopulate_defaults(event): # will repopulate the default text previously inside the entry boxes defined below if # the user does not put anything in while focused and changes focus to another widget if username_box != main.focus_get() and username_box.get() == '': username_box.insert(0, 'Enter Username') elif password_box != main.focus_get() and password_box.get() == '': password_box.insert(0, ' ') def login(*event): # Able to be called from a key binding or a button click because of the '*event' print 'Username: ' + username_box.get() print 'Password: ' + password_box.get() main.destroy() # If I wanted I could also pass the username and password I got above to another # function from here. # defines a grid 50 x 50 cells in the main window rows = 0 while rows < 10: main.rowconfigure(rows, weight=1) main.columnconfigure(rows, weight=1) rows += 1 # adds username entry widget and defines its properties username_box = Entry(main) username_box.insert(0, 'Enter Username') username_box.bind("<FocusIn>", clear_widget) username_box.bind('<FocusOut>', repopulate_defaults) username_box.grid(row=1, column=5, sticky='NS') # adds password entry widget and defines its properties password_box = Entry(main, show='*') password_box.insert(0, ' ') password_box.bind("<FocusIn>", clear_widget) password_box.bind('<FocusOut>', repopulate_defaults) password_box.bind('<Return>', login) password_box.grid(row=2, column=5, sticky='NS') # adds login button and defines its properties login_btn = Button(main, text='Login', command=login) login_btn.bind('<Return>', login) login_btn.grid(row=5, column=5, sticky='NESW') main.mainloop() |
Hello,
How can I create a python tkinter login screen that if the username and password is correct the application will go to a new window. I tried to use hide and show frame but I do not know how to make python go to the next frame if the username and password is correct.
Please help me, I ma new in tkinter.
Thank you
What are you authenticating against, are you trying to test a username and password against a user account on the system, LDAP, or a database local to the application itself?