Search code examples
tkintertkinter-entry

How do i change the text in entry?


from tkinter import *
import string
import random
cisla = string.digits
symboly = string.punctuation
abeceda = string.ascii_letters
heslo = []
root = Tk()
root.title("PASSWORD GENERATOR")

delkahesla = Entry(root, width=35)
delkahesla.insert(0, "How many letters would you like?")
Psymboly = Entry(root, width=35)
Psymboly.insert(0, "How many symbols would you like?")
Pcisla = Entry(root, width=35)
Pcisla.insert(0, "How many numbers would you like?")

def kliknuti():
    x = int(delkahesla.get())
    y = int(Psymboly.get())
    z = int(Pcisla.get())
    for i in range(x):
    heslo.append(random.choice(abeceda))
    for i in range(y):
    heslo.append(random.choice(symboly))
    for i in range(z):
    heslo.append(random.choice(cisla))

    random.shuffle(heslo)
    STRheslo =  ' '.join(heslo)
    vysledek.insert(0, STRheslo)
    return heslo

vysledek = Entry(root, width=35)
napis = Label(root, text="nastaveni")
tlacitko = Button(root, text="Generuj!", command=kliknuti, fg="black", bg="cyan")

napis.pack()
tlacitko.pack()
delkahesla.pack()
Psymboly.pack()
Pcisla.pack()
vysledek.pack()

root.mainloop()

How do i make so when i generate the password the generated password changes in the entry. Now when you click the button more times the passwords just keep adding to the entry im lost and idk what to do. I am a new programmer. Any help is appreciated.


Solution

  • To ensure that the generated password replaces the existing password in the entry widget each time the button is clicked, you should clear the entry widget and the heslo list before generating a new password. Additionally, you should remove the spaces when joining the list into a string. Try something like this:

    from tkinter import *
    import string
    import random
    
    cisla = string.digits
    symboly = string.punctuation
    abeceda = string.ascii_letters
    
    root = Tk()
    root.title("PASSWORD GENERATOR")
    
    def clear_entry(event):
        event.widget.delete(0, END)
    
    delkahesla = Entry(root, width=35)
    delkahesla.insert(0, "How many letters would you like?")
    delkahesla.bind("<FocusIn>", clear_entry)
    
    Psymboly = Entry(root, width=35)
    Psymboly.insert(0, "How many symbols would you like?")
    Psymboly.bind("<FocusIn>", clear_entry)
    
    Pcisla = Entry(root, width=35)
    Pcisla.insert(0, "How many numbers would you like?")
    Pcisla.bind("<FocusIn>", clear_entry)
    
    def kliknuti():
        heslo = []  # Clear the list each time the button is clicked
        vysledek.delete(0, END)  # Clear the entry widget each time the button is clicked
        try:
            x = int(delkahesla.get())
            y = int(Psymboly.get())
            z = int(Pcisla.get())
        except ValueError:
            vysledek.insert(0, "Please enter valid numbers")
            return
    
        for i in range(x):
            heslo.append(random.choice(abeceda))
        for i in range(y):
            heslo.append(random.choice(symboly))
        for i in range(z):
            heslo.append(random.choice(cisla))
    
        random.shuffle(heslo)
        STRheslo = ''.join(heslo)  # Remove spaces when joining the list into a string
        vysledek.insert(0, STRheslo)
        return heslo
    
    vysledek = Entry(root, width=35)
    napis = Label(root, text="nastaveni")
    tlacitko = Button(root, text="Generuj!", command=kliknuti, fg="black", bg="cyan")
    
    napis.pack()
    tlacitko.pack()
    delkahesla.pack()
    Psymboly.pack()
    Pcisla.pack()
    vysledek.pack()
    
    root.mainloop()