如何更改条目中的文本?

问题描述 投票:0回答:1
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()

当我生成密码时,如何使生成的密码在条目中发生更改。现在,当您多次单击该按钮时,密码就会不断添加到条目中,我会丢失并且不知道该怎么办。我是一名新程序员。如有任何帮助,我们将不胜感激。

tkinter tkinter-entry
1个回答
0
投票

为确保每次单击按钮时生成的密码都会替换条目小部件中的现有密码,您需要在生成新密码之前清除条目小部件和 heslo 列表。此外,将列表连接到字符串时需要删除空格。 尝试这样的事情:

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()
© www.soinside.com 2019 - 2024. All rights reserved.