GUI for a random password generator

Question:

I’m learning to use tkinter and ttk so I tried making a GUI for a random password generator and this is my code so far:

import random
from tkinter import *
from tkinter import ttk


win = Tk()

win.geometry("300x250")
win.title('random pass generator')


global password_length
password_length = int()


enter_length = ttk.Entry(win , textvariable = password_length)
enter_length.pack()

global password
password = StringVar()


def generate_password(password_length):
    i = 0
    characters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
    while i < password_length:
        password = password + random.choice(characters)
        i = i + 1

generate = ttk.Button(win,text = 'generate',command = generate_password(password_length))
generate.pack()

label = ttk.Label(win,textvariable = password)
label.config(background = 'black')
label.pack()

win.mainloop()

The problem is that it doesn’t give me the password after I write the password’s length and click the button.

I tried to assign the password to a label to get it on screen but it doesn’t show up after I click the button.

Asked By: R3GUL4R

||

Answers:

As you have password as a StringVar, you need to use the set method to assign it a value, e.g.,:

password.set(password.get() + random.choice(characters))

rather than:

password = password + random.choice(characters)

Also, the command argument to Button can only take a function handle rather than a call to a function, i.e., you could only pass command=generate_password and not command=generate_password(password_length). You can either make password_length global rather than being an argument to the function, hardcode it into the function, or use a lambda function (see here), like:

generate = ttk.Button(
    win,
    text='generate',
    command=lambda: generate_password(password_length)
)

Finally, if using the lambda option above, you should also change:

password_length = int()
enter_length = ttk.Entry(win , textvariable = password_length)

to

password_length = StringVar()
enter_length = ttk.Entry(win, textvariable=password_length)

as textvariable should take a StringVar rather than an int. And then within generate_password, have:

while i < int(password_length.get()):

So, overall the code will be:

import random
from tkinter import *
from tkinter import ttk


win = Tk()

win.geometry("300x250")
win.title('random pass generator')


password_length = StringVar()
enter_length = ttk.Entry(win, textvariable=password_length)
enter_length.pack()

password = StringVar()

def generate_password(password_length):
    i = 0
    characters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
    while i < int(password_length.get()):
        password.set(password.get() + random.choice(characters))
        i = i + 1

generate = ttk.Button(
    win,
    text='generate',
    command=lambda: generate_password(password_length)
)
generate.pack()

label = ttk.Label(win,textvariable = password)
label.config(background='black')
label.pack()

win.mainloop()

Noting that, by setting the label background to black you won’t be able see the password text as it will also be black.

Answered By: Matt Pitkin
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.