How to Make a Random Password Generator in Python

Hello, world, this is A.S. Themidom, today in this post I am gonna show you, how you can make a random password generator, using python. And believe me, it's simple and easy AF.



So, let's get started. We are gonna build a strong password generator, so make sure python is installed in your computer system, and if you want to increase your productivity make sure to have a source code editor, like pycharm, visual studio code, etc.

And also we are going to use Tkinter for making UI, and random module for selecting the random words in a list, and we are going to use string module for selecting the string, character, and numbers. And you need to have a little bit of basic knowledge of python. For any query make sure to comment below.

First, you need to install one python module.

pyperclip. (you can read the documentation from https://pyperclip.readthedocs.io/)

If you are on windows run the following command

 pip install pyperclip  
Or if you are on Linux/macOS then run the below command.
 pip3 install pyperclip  
And our first step is done.
Now it's time to make a good-looking UI, using Tkinter.

So, we are gonna write the below code to make a good-looking UI.
 import time  
 from tkinter import *  
 import random  
 import string  
 import pyperclip  
 class Generator():  
   def __init__(self):  
     root = Tk()  
     root.iconbitmap('icon.ico')  
     root.title('password generator')  
     root.geometry('300x200')  
     self.pas = StringVar()  
     userFrame = LabelFrame(root, text='strong password', font=('Verdana', 10, 'bold'))  
     userFrame.pack(fill="both", expand='yes')  
     left = Message(userFrame, textvariable=self.pas, bg='Black', fg='white')  
     left.config(font=('Helvetica', 10,))  
     left.pack(fill='both', expand='yes')  
     length = Label(root, text='Please give the length in number', font=('verdana', 10), bg='black', fg='white')  
     length.pack(fill='x', expand='no')  
     entry = Entry(root, fg='red', font=('verdana', 10))  
     entry.pack(fill='x', expand='no')  
     btn = Button(root, text='Generate strong password', font=('verdana', 10), bg='deepskyblue', fg='red',  
            ).pack(fill='x', expand='no')  
     btn = Button(root, text='copy it', font=('verdana', 10), bg='yellow', fg='red',  
            ).pack(fill='x', expand='no')  
     btn = Button(root, text='Close', font=('verdana', 10), bg='deepskyblue', fg='red',  command=root.destroy
            ).pack(fill='x', expand='no')  
     root.mainloop()  
 Generator()  
And 2nd step is also done.
The UI may look like this.

Now it's time to write the logic of creating passwords as well as copying the passwords.
 x = ""  
     def copy():  
       global x  
       pyperclip.copy(x)  
       self.pas.set('Text copied')  
     def passw(event=None):  
       global x  
       self.pas.set('ok generating strong password')  
       time.sleep(1)  
       s1 = string.ascii_lowercase  
       s2 = string.ascii_uppercase  
       s3 = string.digits  
       s4 = string.punctuation  
       try:  
         query = int(entry.get())  
         s = []  
         s.extend(list(s1))  
         s.extend(list(s2))  
         s.extend(list(s3))  
         s.extend(list(s4))  
         # self.pas.set(s)  
         # print(s)  
         random.shuffle(s)  
         # self.pas.set(s)  
         # print(s)  
         # print("".join(s[0:query]))  
         x = "".join(s[0:query])  
         self.pas.set(x)  
       except:  
         self.pas.set('Hey man 1st give the length of the password')  
Now, it's time to put the logic into buttons.

Full Code


 import time  
 from tkinter import *  
 import random  
 import string  
 import pyperclip  
 class Generator():  
   def __init__(self):  
     root = Tk()  
     root.iconbitmap('icon.ico')  
     root.title('password generator')  
     root.geometry('300x200')  
     self.pas = StringVar()  
     x = ""  
     def copy():  
       global x  
       pyperclip.copy(x)  
       self.pas.set('Text copied')  
     def passw(event=None):  
       global x  
       self.pas.set('ok generating strong password')  
       time.sleep(1)  
       s1 = string.ascii_lowercase  
       s2 = string.ascii_uppercase  
       s3 = string.digits  
       s4 = string.punctuation  
       try:  
         query = int(entry.get())  
         s = []  
         s.extend(list(s1))  
         s.extend(list(s2))  
         s.extend(list(s3))  
         s.extend(list(s4))  
         # self.pas.set(s)  
         # print(s)  
         random.shuffle(s)  
         # self.pas.set(s)  
         # print(s)  
         # print("".join(s[0:query]))  
         x = "".join(s[0:query])  
         self.pas.set(x)  
       except:  
         self.pas.set('Hey man 1st give the length of the password')  
     userFrame = LabelFrame(root, text='strong password', font=('Verdana', 10, 'bold'))  
     userFrame.pack(fill="both", expand='yes')  
     left = Message(userFrame, textvariable=self.pas, bg='Black', fg='white')  
     left.config(font=('Helvetica', 10,))  
     left.pack(fill='both', expand='yes')  
     length = Label(root, text='Please give the length in number', font=('verdana', 10), bg='black', fg='white')  
     length.pack(fill='x', expand='no')  
     entry = Entry(root, fg='red', font=('verdana', 10))  
     entry.pack(fill='x', expand='no')  
     btn = Button(root, text='Generate strong password', font=('verdana', 10), bg='deepskyblue', fg='red',  
            command=passw).pack(fill='x', expand='no')  
     root.bind('<Return>',passw)  
     btn = Button(root, text='copy it', font=('verdana', 10), bg='yellow', fg='red',  
            command=copy).pack(fill='x', expand='no')  
     btn = Button(root, text='Close', font=('verdana', 10), bg='deepskyblue', fg='red',  
            command=root  
            .destroy).pack(fill='x', expand='no')  
     root.mainloop()  
 Generator()  
Now, you can see the output as.


So, guys now, you can see it's very simple and easy to make a strong password generator.
I tried to make it responsive to the device and added a one-second pause for a professional look, now if you feel it is slowing the program, be free to remove that time.sleep(1) code.
Make sure to share with your programmer friends, and see ya in the next post guys. Thanks for reading the post, and comment for the next topics.
                        -- A.S. themidom

1 Comments

If you have have any doubt please let me know

  1. Merkur Slots Machines - SEGATIC PLAY - Singapore
    Merkur Slot Machines. 5 star rating. 우리카지노 사이트 The 메리트 카지노 주소 Merkur Casino game was the 카지노 first 코인카지노 추천인 to feature video slots in the 카지노 entire casino,

    ReplyDelete
Previous Post Next Post

Ads

Ads