Archive for the ‘GUI’ Category.

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.

UNIX Time Clock, in honor of "1234567890 Day"

On this Friday the 13th, we are to witness a very unique moment in history. That is, it is the day when UNIX time will roll over to 1234567890. Here's a simple Python/Tk app that'll show the timestamp.

import time
from Tkinter import *
 
class TimestampClock(Frame):
    def __init__(self, root):
        Frame.__init__(self, root)
        self.pack()
        self.time = Label(self, text=int(time.time()))
        self.time.config(fg='red', font=('Monospace', 20, 'bold'))
        self.time.pack(padx=10, pady=10)
        self.update()
 
    def update(self):
        self.time.config(text=int(time.time()))
        self.after(1000, self.update)
 
if __name__ == "__main__":
    root = Tk()
    root.title("Timestamp-Clock")
    root.wm_attributes("-topmost", 1)
    root.resizable(0,0)
    root.minsize(300,50)
    TimestampClock(root).mainloop()

I hope you did something special for the occasion. Me? I was sitting in a computer lab at Chemeketa waiting for the clock to hit that special number. Here is my screen grab of this great event:

1234567890