How to create a login page in python
Users can access an application by providing their username and Password or by authenticating with a social media login on the login screen.
Python Tkinter
Python provides a variety of choices for GUI development. Tkinter is the approach used the most frequently among all GUI approaches. It is a standard Python interface for the Python-supplied Tk GUI toolkit. The fastest and most straightforward approach to constructing GUI apps is with Python and Tkinter. Tkinter makes building a GUI a simple process.
Python Tkinter for GUI
One of the most common GUI application components is the login form.
Users that use the Login Form can log in with their user name and Password. The user may be granted privileged access when the credentials have been verified.
Theme Behind the login page created:
In this illustration, we create a program that launches a window with fields for the user name and Password and a submit button.
The user can click the Login button after entering their username and Password.
A function is run to verify the username and Password after clicking the button. You must write your application logic to verify the username and credentials in this function.
For this illustration, we retrieve the user's username and Password from the credential verifying function and print them to the application window.
For starting with the code, we can segregate the program into different sections:
Importing modules section
We have to import the Tkinter module.
We use Tkinter to create the GUI application in python; in this program, we mainly use the Tkinter module, so we first have to import the Tkinter module.
Creating the Mainframe
We will now build the application's primary frame after importing the components.
Designing the Layout
Following the development of the Main Frame, the application will receive some layout. Here we use the grid format to align the labels and input fields.
Initializing the Application
We will now execute the application in the infinite loop so that the main screen can be visible to the user continually until the user exits the page.
Python code for login page
from Tkinter import *
from functools import partial
def validateLogin(username, password):
print ("User has entered the username :", username.get())
print ("User has entered the password :", password.get())
return
# Creating the GUI window
console = Tk () # Initialization of Tkinter
console.geometry('400x300') # Size of the window
console.title('Login page for using python Tkinter code')
# Creating input of username
Label1 = Label (console, text="User Name"). grid (row=0, column=0) # Label to specify Username
Input1 = StringVar()
# Inputing the user’s name
usernameEntry = Entry (console, text variable = Input1). grid (row=0, column=1)
# Creating input of Password
# Label to specify Login
Label2 = Label(console,text="Password").grid(row=1, column=0)
Input2 = StringVar()
# Inputing the Password
passwordEntry = Entry (console, text variable = Input2, show='*'). grid (row=1, column=1)
validateLogin = partial (validateLogin, Input1, Input2)
# Creating login button
loginButton = Button (console, text="Login", command=validateLogin). grid (row=4, column=0)
console.mainloop()
The above is used to create the login page with a GUI, which inputs the two String inputs one is a username, and the other is a Password. And we can validate the user by the two inputs taken using validate function.
When this code is executed, the output screen is the simple user interface that inputs the username and Password and contains a login button. On clicking the login button after entering the username and Password and in the above code, we have printed the username and Password entered by the user.
The following is the output window after executing the above-given code:
Here we are taking the two inputs:
Username and Password for verifying the credentials of a user.
Here the username and Password are printed on the output console after entering the user credentials, that is, username and password, using the general print method in python. The following is the picture of the output window after entering the username and Password and clicking the login button.
To display stars when the user types the Password, we used Entry (.., show = '*') for the Password.
Of course, you can enhance the labels and GUI window. However, because the main focus of this tutorial is on the functionality of a login form, we continue with the standard style of GUI elements.