Reputation: 1
When I submit the desired password length, the length doesn't change.
There are two buttons: the top one (which is smaller), is for submitting the password's length. The bottom one (bigger), generates the password, taking the length that was inputted.
The default length is 12 characters, though I will make the minimum length 8, and the maximum 16.
def copy():
copy_pw = Tk()
copy_pw.withdraw()
copy_pw.clipboard_clear()
copy_pw.clipboard_append(password)
copy_pw.update()
def password_generator():
lower_case = "abcdefghijklmnopqrstuvwxyz"
upper_case = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
numbers = "0123456789"
symbol = "%^#*:;._-@`~"
answer = lower_case + upper_case + numbers + symbol
global password_length
password_length = 12
global password
password = "".join(random.sample(answer, password_length))
print("Password has been generated: ", password)
text.config(text = password)
def submit_length():
user_length = entry.get()
password_length = user_length
window = Tk()
window.title("Password Generator")
length = Button(window, text = 'Enter')
length.pack()
length.config(command = submit_length)
length.config(font =('Segoe UI', 10))
length.config(bg = '#009DFF')
length.config(fg = '#ffffff')
length.config(activebackground = '#009DFF')
length.config(activeforeground = '#ffffff')
entry = Entry()
entry.pack()
entry.config(font = ('Segoe UI', 12))
button = Button(window, text = 'Generate password')
button.pack()
button.config(command = password_generator)
button.config(font =('Segoe UI', 22))
button.config(bg = '#009DFF')
button.config(fg = '#ffffff')
button.config(activebackground = '#009DFF')
button.config(activeforeground = '#ffffff')
text = Label(window, text = password)
text.pack()
text.config(font = ('Monospace', 25))
button.pack()
# copy password
copy_password = Menu(text, tearoff= 0, bg = "white", fg = "black")
copy_password.add_command(label="Copy", command=copy)
# popup on right click
text.bind("<Button - 3>", popup)
window.mainloop()
Upvotes: 0
Views: 73
Reputation: 781088
You need to declare the variable global in the function that assigns it, and convert to integer.
def submit_length():
global password_length
user_length = entry.get()
password_length = int(user_length)
Upvotes: 0