Creating a Simple Password Generator in Python

Language

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

  1. from tkinter import *
  2. 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.

  1. root = Tk()
  2. root.title("Sourcecodester")
  3. width = 400
  4. height = 200
  5. screen_width = root.winfo_screenwidth()
  6. screen_height = root.winfo_screenheight()
  7. x = (screen_width/2) - (width/2)
  8. y = (screen_height/2) - (height/2)
  9. root.geometry("%dx%d+%d+%d" % (width, height, x, y))

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

  1. #=====================================METHODS===================================
  2. def Random():
  3. alphabet = "abcdefghijklmnopqrstuvwxyz"
  4. length = 8
  5. new_password = ""
  6.  
  7. for i in range(length):
  8. next_index = random.randrange(len(alphabet))
  9. new_password = new_password + alphabet[next_index]
  10.  
  11.  
  12. for i in range(random.randrange(1,3)):
  13. replace_index = random.randrange(len(new_password)//2)
  14. new_password = new_password[0:replace_index] + str(random.randrange(10)) + new_password[replace_index+1:]
  15.  
  16.  
  17. for i in range(random.randrange(1,3)):
  18. replace_index = random.randrange(len(new_password)//2,len(new_password))
  19. new_password = new_password[0:replace_index] + new_password[replace_index].upper() + new_password[replace_index+1:]
  20.  
  21. PASSWORD.set(new_password);

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.

  1. #====================================VARIABLES==================================
  2. PASSWORD = StringVar()
  3.  
  4. #====================================FRAME======================================
  5. Top = Frame(root, width=width)
  6. Top.pack(side=TOP)
  7. Form = Frame(root, width=width)
  8. Form.pack(side=TOP)
  9.  
  10. #====================================LABEL WIDGET===============================
  11. lbl_title = Label(Top, width=width, font=('arial', 16), text="Python:Password Generator", bd=1, relief=SOLID)
  12. lbl_title.pack(fill=X)
  13. lbl_password = Label(Form, font=('arial', 18), text="Password", bd=10)
  14. lbl_password.grid(row=0, pady=15)
  15.  
  16. #====================================ENTRY WIDGET===============================
  17. password = Entry(Form, textvariable=PASSWORD, font=(18), width=16)
  18. password.grid(row=0, column=1)
  19.  
  20. #====================================BUTTON WIDGET==============================
  21. btn_generate = Button(Form, text="Generate", width=20, command=Random)
  22. btn_generate.grid(row=1, columnspan=2)

Demo

There you have it! We have 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!!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Comments

Submitted byThomas1965 (not verified)on Thu, 05/04/2017 - 19:09

Nice tutorial, but one important line missing so it doesn't show the GUI. Top.mainloop() should be added at the end

Add new comment