Skip to content
Home » Blog » How to make login interface using python

How to make login interface using python

Introduction:

Today in this blog we are making the login interface in a simple way with the help of Python. it is easier and simple to make. We need Pycharm and Python to make this.

Importing the modules:

As we all know, we need Python modules for making every python project. For this project we Tkinter module. Tkinter is a standard Python library for creating graphical user interfaces (GUI). It provides tools that allow developers to create windows, buttons, labels, textboxes, and other GUI components.

from tkinter import *
from tkinter import messagebox

Calling a function:

Def helps to call a function. Now, we should mention the user name and password. We called the function named

def Login():
 username = entry1.get()
 password = entry2.get()

Creating the condition:

Now we are making the condition for login. We use “if ” code for making conditions and “elif” is also used to make other users available. We are also using “else” function because if the above condition doesn’t meet, it will work.

if username == "" and password == "":
 messagebox.showinfo("Empty box are not valid")
elif username == "admin" and password == "admin":
 messagebox.showinfo("Welcome" + username + "!")
else:
  messagebox.showinfo("Invilad username or password")

You can add other usernames and passwords using “elif ” function as above. you can replace yourname or other user namer in username ==”add your name ” and password == “as your wish”. you can also change the message. To change the message you need to go to messagebox.showinfo(“add your message here”).\

Making the interface:

Now, we are making the interface. Let’s see how it is done

root = Tk()
root.geometery("300x200")
root.title("Login")

You can change parameters using root.geometry(“your parameter”). Remember the first parameter is length and the second is breadth. Don’t forget to use “x” between the two parameter

Creating the entry box:

Now we are using an entry box for the entry of username and password.

global entry1
global entry2

Label(root, text="Username").place(x=20, y=20)
Label(root, text="Password").place(x=20, y=70)

entry1 = Entry(root, bd=5)
entry1.place(x=140, y=20)

entry2 = Entry(root, bd=5)
entry2.place(x=140, y=70)

Button(root, text="Login", command=Login, height=3, width=13, bd=6).place(x=100, y=120)
root.mainloop()

exit()

We created the whole thing now.

Whole code:

from tkinter import *
from tkinter import messagebox

def Login():
   username = usernameEntry.get()
   password = passwordEntry.get()
   if username == "" and password == "":
       messagebox.showinfo("Message", "Welcome, " + username + "!")
   elif username == "admin" and password == "admin":
       messagebox.showinfo("Message", "Welcome, " + username + "!")
   else:
       messagebox.showerror("Message", "Invalid username or password")


root = Tk()
root.geometry("300x200")
root.title("Login")

global entry1
global entry2

Label(root, text="Username").place(x=20, y=20)
Label(root, text="Password").place(x=20, y=70)

entry1 = Entry(root, bd=5)
entry1.place(x=140, y=20)

entry2 = Entry(root, bd=5)
entry2.place(x=140, y=70)

Button(root, text="Login", command=Login, height=3, width=13, bd=6).place(x=100, y=120)
root.mainloop()

exit()

Now let’s see the output of this.

Login Interface:

This is the login interface. We have created using Python. It is a simple method.

In this image, it show the output of that code.

Try login without any username and password:

It shows like this.

Let’s try with the correct username and password:

then it will show the message like this.

Let’s try with an incorrect username or password:

It gives a message like this.

We can add more users and one more thing in this user. We will discuss this in another blog.

Read more:

Which is better python or java?
Learn more about python

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *