How to create Login Form using Python Tkinter?

Python Tkinter – Login Form

Login Form is one of the most used in GUI applications.

Login Form helps users to login using user name and password. Once the credentials are validated, user can be given privileged access.

Example Program

In this example, we write a program that opens a window with fields: username and password and a button to submit these values.

User can enter the details for username, password; and click on Login button.

Once the button is clicked, a function is called to validate the credentials. Here, in this function, you have to write your business logic to validate the username and password.

For this example, in the credential validating function, we get the username and password entered by the user, and print it to the console.

Python Program

from tkinter import *
from functools import partial

def validateLogin(username, password):
	print("username entered :", username.get())
	print("password entered :", password.get())
	return

# Window
tkWindow = Tk()  
tkWindow.geometry('400x150')  
tkWindow.title('Tkinter Login Form - pythonexamples.org')

# Username label and text entry box
usernameLabel = Label(tkWindow, text="User Name").grid(row=0, column=0)
username = StringVar()
usernameEntry = Entry(tkWindow, textvariable=username).grid(row=0, column=1)  

# Password label and password entry box
passwordLabel = Label(tkWindow,text="Password").grid(row=1, column=0)  
password = StringVar()
passwordEntry = Entry(tkWindow, textvariable=password, show='*').grid(row=1, column=1)  

validateLogin = partial(validateLogin, username, password)

# Login button
loginButton = Button(tkWindow, text="Login", command=validateLogin).grid(row=4, column=0)  

tkWindow.mainloop()
Copy

Output

Following video demonstrates the output and usage of above Python program.

We have used Entry(.., show='*') for password, to show stars when user types the password.

You can beautify the labels and GUI window of course. But, as this tutorial is mainly focused on the functionality of a Login Form, we stick to the default styling of GUI elements.

Summary

In this tutorial of Python Examples, we learned to create a Login Form in Python GUI application using tkinter.

Related Tutorials

Code copied to clipboard successfully 👍