In this article, we are going to create a Graphical User Interface based OTP generator application in Python. We will see how to create a GUI app using Tkinter and how to add functionality to it.
In this article, we are going to create a Graphical User Interface based OTP generator application in Python. We will see how to create a GUI app using Tkinter and how to add functionality to its front end. We will create an app that will generate an application that will generate a One Time Password (OTP) of 4 digits and display it on the application whenever the "Generate OTP" button is clicked.
We will see how to design a basic GUI for the application and how to add functionality to it. Let's divide the code into 2 parts 1st the frontend GUI part and 2nd the backend functionality part.
As we will be implementing the GUI using Tkinter we will import the following libraries:
from tkinter import * from tkinter import ttk
Now we will create a class otp and define the main function. Give a title to the window and set the window geometry as "1920x1080" as the size and "+0+0" are the starting coordinates from where the window will start. You can change these parameters as per your choice.
class otp: def __init__(self,window): self.window = window self.window.title("One Time Password login system") self.window.geometry("1920x1080+0+0")
Now we will create a frame in self.window as shown:
frame1 = Frame(self.window,bd=1,relief=RIDGE,bg="skyblue") frame1.place(x=20,y=70,height=660,width=1300)
we'll create a label and a button in the 'frame1' that we created earlier.
label1 = Label(frame1,text="Click on button to generate OTP ",bg="white",fg="black",font=("times new roman",20,"bold")) label1.grid(row=4,column=2,padx=450,pady=50,sticky=W) generate_button = Button(frame1,text="Generate OTP",height=2,width=11, command=generate_OTP) generate_button.grid(row=5, column=2,pady=20,padx=10)
We will give window.mainloop to enable the window.
Import random library because we'll be using it for otp generation. Create a function and in that create an empty list. Now 4 variables 'a,b,c and d' that will store random single-digit numbers from 0 to 9.
def generate_OTP(): list1 = [] a = random.randint(0,9) b = random.randint(0,9) c = random.randint(0,9) d = random.randint(0,9)
Now append all the variables one by one to the list created earlier and store the list in a variable called "otp" so that it can be used as it is later.
list1.append(a) list1.append(b) list1.append(c) list1.append(d) otp = list1
Now that we have generated the OTP we will now display it using the Label function.
label2 = Label(frame1,text=otp,bg="white",fg="black",font=("times new roman",20,"bold")) label2.grid(row=6,column=2,padx=600,pady=50,sticky=W)
Lastly in the Button "Generate OTP" we will add 'command=generate_OTP' so that whenever the button is clicked the function is called and the OTP is generated.
import random from tkinter import * from tkinter import ttk class otp: def __init__(self,window): self.window = window self.window.title("One Time Password login system") self.window.geometry("1920x1080+0+0") title = Label(self.window,text="OTP login system",bd=0,font=("times new roman",30,"bold"),bg="white",fg="purple") title.pack(side=TOP,fill=X) def generate_OTP(): list1 = [] a = random.randint(0,9) b = random.randint(0,9) c = random.randint(0,9) d = random.randint(0,9) list1.append(a) list1.append(b) list1.append(c) list1.append(d) otp = list1 label2 = Label(frame1,text=otp,bg="white",fg="black",font=("times new roman",20,"bold")) label2.grid(row=6,column=2,padx=600,pady=50,sticky=W) frame1 = Frame(self.window,bd=1,relief=RIDGE,bg="skyblue") frame1.place(x=20,y=70,height=660,width=1300) label1 = Label(frame1,text="Click on button to generate OTP ",bg="white",fg="black",font=(20)) label1.grid(row=4,column=2,padx=450,pady=50,sticky=W) generate_button = Button(frame1,text="Generate OTP",height=2,width=11, command=generate_OTP) generate_button.grid(row=5, column=2,pady=20,padx=10) window = Tk() ob = otp(window) window.mainloop()
You can also convert this Python file to a .exe file so that it can run as an application.
Feel free to do any customizations or modifications.
Submitted by Anay Ravindra Karanje (Anay250)
Download packets of source code on Coders Packet
Comments