How to generate different password each time i clicked the button python

123 Views Asked by At

I have 2 python files: 1, the password generator, and 2, the password manger.

In the password manger I am using tkinter. I want to retrieve a generated password each time I click on the the generate button that imports from password generator file. however each time each I clicked on the generate button the same password shows.

Is there a way to generate a different password each time I click on the generate button?

File 1: password generator:

#Password Generator Project
from random import  shuffle,choice,randint
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbols = ['!', '#', '$', '%', '&', '(', ')', '*', '+']



password_list = []

password_list = [choice(letters) for char in range(randint(8,10))]

password_list += [choice(symbols) for char in range(randint(2,4))]



password_list += [choice(numbers) for char in range(randint(2,4))]


shuffle(password_list)

password = "".join(password_list)


file 2: password manger(PASSWORD GENERATOR Function):

from tkinter import  *
from tkinter import messagebox
import pyperclip
YELLOW = "#f7f5dd"

# ---------------------------- PASSWORD GENERATOR ------------------------------- #
def GENERATOR_PASSWORD():
    password_input.delete(0,END)
    import password_gen
    generated_password = password_gen.password
    password_input.insert(0,generated_password)
    pyperclip.copy(generated_password)

1

There are 1 best solutions below

0
On

Is there a way to generate a different password each time I click on the generate button?

Certainly.

First, wrap your password generator in a function instead of having just a single global password in the module. (I took the liberty of shortening it a bit by using the constants already in the Python standard library...)

from random import shuffle, choice, randint
import string

letters = string.ascii_letters
numbers = string.digits
symbols = '!#$%&()*+'


def generate_password():
    password_list = [choice(letters) for char in range(randint(8, 10))]
    password_list += [choice(symbols) for char in range(randint(2, 4))]
    password_list += [choice(numbers) for char in range(randint(2, 4))]
    shuffle(password_list)
    return "".join(password_list)

Secondly, call that function in your password manager.

import password_gen

def GENERATOR_PASSWORD():
    password_input.delete(0, END)
    generated_password = password_gen.generate_password()
    password_input.insert(0, generated_password)
    pyperclip.copy(generated_password)