Back to the drawing board…

Undaunted by my ignoble initial defeat at the hands of the Silver Mystery Table, I decided to do something difficult: admit that perhaps I bit off more than I can chew, and go back to some basic Tkinter practice, including improving my understanding of Python classes. I know just enough about the concept of a class to appreciate how powerful it is, but not quite enough to pull off something useful.

Inspired by this lesson at TkDocs, I decided to have some fun by writing an application that makes people cool!  Silly, yes, but I found this is how I learn best..study basic concepts, see an example, then try it out on an idea of my own.

Since I like being surrounded by cool people, and frequently find many in need of coolness, I want to make my app very easy to use. It will prompt the user for the name of a person to be made cool, make them cool, and display a congratulatory message. Since I will probably want to use this function frequently, I’m going to put it in a module I will name “cool_tools.”

Of course, since I already know many cool people, I wouldn’t want to accidentally have my coolness function used on one of them. Not only would attempting to apply coolness to someone who is already cool insult them, it would cause the app to crash in an embarrassing way. Therefore I will need to include some code to prevent this grim scenario.

Here it is:

def coolness(name):
    """ This function makes people cool.  It will return an
            error if you attempt to use it on someone who is
            already cool."""
    # List of people who are already cool. This is a tuple, meaning
    # these individuals are unchangeably cool.
    cool_people = ("tony",
                   "kevin",
                   "bryan",
                   "matt",
                   "victor",
                   "chris",
                   "ricardo")
    for cool_person in cool_people:
        # Loops through the list of cool people, in case
        # the user inadvertently entered the name of someone
        # who is already cool.
        if cool_person == name.lower():
            result = "ERROR! Invalid usage. {} is already cool.".format(name)
            break
        else:
            result = "{} is now a cool person! CONGRATULATIONS, {}!"\
                     .format(name,name)
    return result

So, let’s test it, shall we? First, I run the module, which loads and leaves me at a Python command prompt. (I use IDLE for my coding). Then, I try it on an uncool person, and then a cool person. Result:

>>> print coolness(“Rob”)
Rob is now a cool person! CONGRATULATIONS, Rob!
>>> print coolness(“Tony”)
ERROR! Invalid usage. Tony is already cool.
>>>

It works! So, I save this function in a module called “cool_tools.py”. This means it can be imported whenever needed, and act just like a for real, serious Python tool created by a pro! It even has a “help” option (a/k/a a “docstring” — the double-quoted comment at the beginning). Here how that works:

>>> from cool_tools import coolness
>>> help(coolness)
Help on function coolness in module cool_tools:

coolness(name)
This function makes people cool. It will return an
error if you attempt to use it on someone who is
already cool.
>>>

So, now I have my very own Python function. But it’s just that — a function. It must be called from a programming environment such as IDLE, or summoned with a “python” statement from the dark, scary terminal window. It can’t rightly be called an application — yet.

What I really want is GUI application that uses this function, and looks as cool as it is! Following the example of the above TkDocs lesson, I sketch out how I’d like my app to look:

coolness_sketch

Very simple: an entry box for the name of the person to be made cool, a button to activate the coolness, and a text box below to display the result. So, without further ado, here is Apply Coolness 1.0:

# Apply Coolness 1.0
# Import my cool function first!
from cool_tools import coolness
from Tkinter import *
import ttk

# Create the root window
root = Tk()
root.title("Apply Coolness")

# Create the frame to hold the boxes & button
mainframe = ttk.Frame(root)

# Set the variable for the name entered by the user
name = StringVar()

# Label that tells the user what to do.
lblPrompt = ttk.Label(text="Enter the name of the person \
                        to be made cool:")
lblPrompt.pack()

# Box in which user enters a person's name.
txtEntry = ttk.Entry(mainframe,textvariable=name)
txtEntry.pack()

# The button that activates the coolness function.
btnCool = ttk.Button(mainframe,text="Make Cool",command=coolness(txtEntry.get()))
btnCool.pack()

# Box that displays the result of the procedure.
txtResult = Text(mainframe)
txtResult.pack()

# Set the focus to the entry box so the user won't have to
# click on it first.
txtEntry.focus()

root.mainloop()

…and here’s the result…

coolness_apply coolness10

Whoops..definitely not cool! How can it be fixed? Stay tuned!