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:

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

