Generate random password

Hello! :wave:
I was trying to make a project that generates random passwords.

from tkinter import *
import random
import string

# recourses&def
string.digits
string.ascii_letters
abc  = random.choice(string.ascii_letters)
num  = random.choice(string.digits)
pun  = random.choice(string.punctuation)
abc2 = random.choice(string.ascii_letters)
num2 = random.choice(string.digits)
pun2 = random.choice(string.punctuation)
abc3 = random.choice(string.ascii_letters)
num3 = random.choice(string.digits)
pun3 = random.choice(string.punctuation)
abc4 = random.choice(string.ascii_letters)
num4 = random.choice(string.digits)
pun4 = random.choice(string.punctuation)

password = abc+num+pun+abc2+"  "+num2+pun2+abc3+num3+"  "+pun3+abc4+num4+pun4


def myclick():
    print(password)
    label2 = Label(window, text="    ")
    label2.config(text=password,font=("Serif", 50, "bold"),bg="#bfbcbb")
    label2.config()
    label2.pack()
    


# window
window = Tk()
window.title("Random PasswordS")



# the label
label = Label(window, text=" Welcome To Random Passwords ")
label.config(font=("Ink Free", 50, "bold"))
label.config(fg="#fc0331")
label.config(bg="#03fce7")
label.grid(row=0, column=0)

# button
theButton = Button(window, text="Click here!",
                   padx=20, pady=5, command=myclick)
theButton.grid(row=1, column=0)


# mainloop
window.mainloop()



the problem is I want the password to be replaced with new one when I click the button again but I don’t know what function I should use and I want to make the label “Welcome To Random Passwords” to be in the center of window’s top. I tried to use

label.pack()

but then another problem appeared as when I click the button again the password is printed again in a new label under the first one.
The same code but using “label.pack()” instead of "label.grid(row=0, column=0)
"

from tkinter import *
import random
import string

# recourses&def
string.digits
string.ascii_letters
abc  = random.choice(string.ascii_letters)
num  = random.choice(string.digits)
pun  = random.choice(string.punctuation)
abc2 = random.choice(string.ascii_letters)
num2 = random.choice(string.digits)
pun2 = random.choice(string.punctuation)
abc3 = random.choice(string.ascii_letters)
num3 = random.choice(string.digits)
pun3 = random.choice(string.punctuation)
abc4 = random.choice(string.ascii_letters)
num4 = random.choice(string.digits)
pun4 = random.choice(string.punctuation)

password = abc+num+pun+abc2+"  "+num2+pun2+abc3+num3+"  "+pun3+abc4+num4+pun4


def myclick():
    print(password)
    label2 = Label(window, text="    ")
    label2.config(text=password,font=("Serif", 50, "bold"),bg="#bfbcbb")
    label2.pack()
    


# window
window = Tk()
window.title("Random PasswordS")



# the label
label = Label(window, text=" Welcome To Random Passwords ")
label.config(font=("Ink Free", 50, "bold"))
label.config(fg="#fc0331")
label.config(bg="#03fce7")
label.pack()

# button
theButton = Button(window, text="Click here!",
                   padx=20, pady=5, command=myclick)
theButton.grid(row=1, column=0)
#theButton.pack()


# mainloop
window.mainloop()


could someone help me with this? :sweat_smile:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.