Python - Create User Password

In this tutorial we will create a Create User Password using Python. This code will generate a random default password for user when the generate button is clicked. The code use tkinter module to create a layout and widgets that can call python functions. When a function is called it the application will automatically generate a random character by undergoing in a for loop base on the given length. This a free useful program, feel fee to modify it and use it on your working projects. We will be using Python because it has a design philosophy which emphasizes code readability. That's why python is very easy to use especially for beginners who just started programming. It is very easy to learn the syntax emphasizes readability and it can reduces time consuming in developing..

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. #====================================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. #====================================LABEL WIDGET===============================
  10. lbl_title = Label(Top, width=width, font=('arial', 16), text="Python - Create User Password", bd=1, relief=SOLID)
  11. lbl_title.pack(fill=X)
  12. lbl_user = Label(Form, font=('arial', 14), text="Generating User Password", bd=10)
  13. lbl_user.grid(row=0, columnspan=2)
  14. lbl_password = Label(Form, font=('arial', 18), text="Password", bd=10)
  15. lbl_password.grid(row=1)
  16.  
  17. #====================================ENTRY WIDGET===============================
  18. password = Entry(Form, textvariable=PASSWORD, font=(18), width=16)
  19. password.grid(row=1, column=1)
  20. password.config(state='readonly')
  21. #====================================BUTTON WIDGET==============================
  22. btn_generate = Button(Form, text="Generate", width=20, command=Generate)
  23. btn_generate.grid(row=2, columnspan=2)

Creating the Main Function

This is where the code that contains the main functions. This code will generate random user password when the button is clicked. To do that just copy and write these blocks of code.
  1. #=====================================METHODS===================================
  2. def Generate():
  3.     chars = "abcdefghijklmnopqrstuvwxyz"
  4.     length = 12
  5.     password = ""
  6.  
  7.     for i in range(length):
  8.         next_index = random.randrange(len(chars))
  9.         password = password + chars[next_index]
  10.  
  11.  
  12.     for i in range(random.randrange(1,6)):
  13.         replace_index = random.randrange(len(password)//2)
  14.         password = password[0:replace_index] + str(random.randrange(10)) + password[replace_index+1:]
  15.  
  16.  
  17.     for i in range(random.randrange(1,6)):
  18.         replace_index = random.randrange(len(password)//2,len(password))
  19.         password = password[0:replace_index] + password[replace_index].upper() + password[replace_index+1:]
  20.  
  21.     PASSWORD.set(password);
There you have it we just created a Create User Password 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!!

Add new comment