-1

So i just made a password generator but everytime i generate a new one the label doesnt updates,it just adds a new password in the window keeping the previous ones

import random
import string
import time
from tkinter import *


def pass_generator():
    length = textBox.get()
    characters = list(string.ascii_letters + string.digits + "!@#$%^&*()")
    password = []
    display_pass = StringVar()

    while len(password) < int(length):
        password.append(random.choice(characters))
        display_pass.set("".join(password))

   print("This is your password: \n")
   print("".join(password))
   Label(window, textvariable=display_pass).pack()

# with open("passwords.txt", "a") as f:
#     date = time.strftime("%d/%m/%Y %H:%M:%S")
#     f.write(f"{date}: " + "".join(password) + "\n")


window = Tk()
window.geometry("500x150")
textBox = IntVar()
text = Label(window, text="Password length:").pack(side=LEFT)
length_entry = Entry(window, textvariable=textBox).pack(side=RIGHT)
button1 = Button(window, text="Generate", command=pass_generator).pack(side=BOTTOM)
window.mainloop()

I want to replace the current password with the new generated one but i cant get to do that

Nakama
  • 19
  • 4
  • 1
    Each time the user presses the button, it calls `pass_generator`. `pass_generator` creates a new `Label` and `pack`s it in the window. That is why it keeps creating more and more `Label`s. Also your code has a few problems like [this](https://stackoverflow.com/q/1101750/11106801) which can show themselves later on. – TheLizzard Sep 08 '22 at 09:49
  • It worked for me. I'm using Python 3.11.0rc1 – toyota Supra Sep 08 '22 at 09:51
  • This is output: This is your password: w This is your password: wM This is your password: wMe This is your password: wMeE This is your password: wMeEi This is your password: S This is your password: Ss This is your password: Ssn This is your password: Ssn3 This is your password: – toyota Supra Sep 08 '22 at 09:55
  • 1
    @toyotaSupra yeah that was for me to test it looking at the terminal, my bad – Nakama Sep 08 '22 at 09:57

1 Answers1

2

It is because you create new label for the new generated password. You need to create the result label once outside the function and update the result label inside the function:

import random
import string
from tkinter import *

def pass_generator():
    length = textBox.get()
    characters = list(string.ascii_letters + string.digits + "!@#$%^&*()")
    password = "".join(random.choice(characters) for _ in range(int(length)))
    # below is to generate password without duplicate characters
    #random.shuffle(characters)
    #password = "".join(characters[:int(length)])

    print("This is your password:", password)
    result.config(text=password) # show the password

window = Tk()
window.geometry("500x150")
textBox = IntVar()
Label(window, text="Password length:").pack(side=LEFT)
Entry(window, textvariable=textBox).pack(side=RIGHT)
Button(window, text="Generate", command=pass_generator).pack(side=BOTTOM)
# label for the generated password
result = Label(window)
result.pack()
window.mainloop()
acw1668
  • 40,144
  • 5
  • 22
  • 34
  • omg thank you so much, it works. Could you explain me what "".join(random.choice(characters) for _ in range(int(length))) does? i dont get the for _ in range... – Nakama Sep 08 '22 at 11:10
  • 1
    `random.choice(characters) for _ in range(int(length))` is [*list comprehensions*](https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions) to create a list using for loop in one line. – acw1668 Sep 08 '22 at 11:35