Create Password Generator using Phyton

Tags

In this tutorial we will create A Simple Password Generator Using Python. Python has a design philosophy which emphasizes code readability. Python is very easy to learn the syntax emphasizes readability and it can reduces time consuming in developing. You can use this application for getting your random password to a certain account So let's now do the coding.
Getting started
First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.
Importing Modules
After setting up the installation and the database, run the IDLE and click file and then new file. After that a new window will appear containing a black file this will be the text editor for the python.
Then copy code that I provided below and paste it inside the IDLE text editor
from tkinter import *
import random
Setting up the Main Frame
After importing the modules, we will now then create the main frame for the application. To do that just copy the code below and paste it inside the IDLE text editor.
root = Tk()
root.title("Sourcecodester")
width = 400
height = 200
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x = (screen_width/2) - (width/2)
y = (screen_height/2) - (height/2)
root.geometry("%dx%d+%d+%d" % (width, height, x, y))
Designing the Layout
After creating the Main Frame we will now add some layout to the application. Just kindly copy the code below and paste it inside the IDLE text editor.

#====================================VARIABLES==================================
PASSWORD = StringVar()
 
#====================================FRAME======================================
Top = Frame(root, width=width)
Top.pack(side=TOP)
Form = Frame(root, width=width)
Form.pack(side=TOP)
#====================================LABEL WIDGET===============================
lbl_title = Label(Top, width=width, font=('arial', 16), text="Python:Password Generator", bd=1, relief=SOLID)
lbl_title.pack(fill=X)
lbl_password = Label(Form, font=('arial', 18), text="Password", bd=10)
lbl_password.grid(row=0, pady=15)
 
#====================================ENTRY WIDGET===============================
password = Entry(Form, textvariable=PASSWORD, font=(18), width=16)
password.grid(row=0, column=1)
 
#====================================BUTTON WIDGET==============================
btn_generate = Button(Form, text="Generate", width=20, command=Random)
btn_generate.grid(row=1, columnspan=2)
Creating the Main Function
This is the main function where the Entry generate a random character when button is cliked. To do that just simply copy the code below then paste it inside the IDLE text editor

#=====================================METHODS===================================
def Random():
    alphabet = "abcdefghijklmnopqrstuvwxyz"
    length = 8
    new_password = ""

    for i in range(length):
        next_index = random.randrange(len(alphabet))
        new_password = new_password + alphabet[next_index]
 
    for i in range(random.randrange(1,3)):
        replace_index = random.randrange(len(new_password)//2)
        new_password = new_password[0:replace_index] + str(random.randrange(10)) + new_password[replace_index+1:]
 
    for i in range(random.randrange(1,3)):
        replace_index = random.randrange(len(new_password)//2,len(new_password))
        new_password = new_password[0:replace_index] + new_password[replace_index].upper() + new_password[replace_index+1:]

    PASSWORD.set(new_password);


There you have it we created a Simple Password Generator Using Python. I hope that this simple tutorial help you for what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!


EmoticonEmoticon