Random password generator in Python and Tkinter

This is always a fun project. The task? To create a random password of random length. The reason for a password generator obvious: you suck at choosing a password. Let's start with how to create the actual generator, and then we'll focus on the presentation.

from random import *
import string
 
# The characters to make up the random password
chars = string.ascii_letters + string.digits
 
def random_password():
    """ Create a password of random length between 8 and 16
        characters long, made up of numbers and letters.
    """
    return "".join(choice(chars) for x in range(randint(8, 16)))

The above function is pretty easy to follow. What it does is build a generator object that creates a random list of characters between 8 and 16 in length. It then just compresses the list into a string. You can do something as simple as print random_password() and it'll display a password in the terminal. This, of course, isn't really the best way of acheiving it. After all, you don't want to have to navigate the terminal each and every time. So, let's add in a graphical user interface using the Tkinter window builder:

from Tkinter import *
from random import *
import string
 
# The characters to make up the random password
chars = string.ascii_letters + string.digits
 
def random_password():
    """ Create a password of random length between 8 and 16
        characters long, made up of numbers and letters.
    """
    return "".join(choice(chars) for x in range(randint(8, 16)))
 
#
# BEGIN GUI CODE
#
 
root = Tk()
root.title("Password Generator")
root.resizable(0,0)
root.minsize(300,0)
 
frame = Frame(root)
frame.pack(pady=10, padx=5)
 
content = StringVar()
updater = lambda:content.set(random_password())
 
gen_btn = Button(frame, text="Generate", command=updater)
gen_btn.config(font=("sans-serif", 14),  bg="#92CC92")
gen_btn.pack(side=LEFT, padx=5)
 
field = Entry(frame, textvariable=content)
field.config(fg='blue', font=('courier',  16, "bold"), justify='center')
field.pack(fill=BOTH, side=RIGHT, padx=5)
 
root.mainloop()

The above should be pretty simple to follow. As you can see, pressing the gen_btn activates the updater lambda function which populates the entry field. Here is a sample output:

password generator

Simple, clean, and easy. This is just a quick project I made for myself and decided to share it.

 

 

2 Comments

  1. Rod wrote,

    Evan,

    I have been learning Python for a few months, and I have just discovered this blog. Lots of interesting material! Just a few brief questions:

    1) how do you publish the Python code?

    2) I suppose your IDE has a "print2html"-like functionality, right? Which IDE do you use?

    It's cool that your code has the same highlighting as IDLE. Most IDE's I have come across which allow one to print code to HTML format don't have this sexy highlighting by default.

    Any input would be most welcome! Cheers!

  2. Evan wrote,

    @Rod,

    I use SciTE as my main text editor. It is small and simple, but it can be a bit difficult to configure to your needs.

    The syntax highlighting you see on this page actually is from a Wordpress plugin called WP-Syntax. It's incredibly useful.
    Of course, if you need any more assistance, don't hesitate to contact me.

    Regards, - Evan

Leave a comment