Coolness to the people! [resuming my tinkering with Tkinter]

In my quest to perfect a data-centric GUI app, I have chosen Tkinter as my GUI tool set of choice.  I’ve seen it criticized for being ancient. Heck, according to this, Tkinter was invented in 1994, and the Tk toolkit it came from goes all the way back to 1988! On the other hand, the way I see it, the fact that it’s still used at all is a testimony to its staying power. How many other software tools from 1988 are still around? Ironically, Tkinter is compatible with Python 3, while some competing, newer GUI toolkits such as wxPython are not. I also like Tkinter because it comes with Python — no special downloads or installations required.

Speaking of Python 3, I realize there’s controversy on how to get the Python world fully migrated to Python 3, but it seems there is agreement that Python 3 is the direction we need to move in sooner or later. Therefore, being a newbie, I have decided to go with learning Python 3 for my own coding. I want to save myself the grief of learning how to do everything I ever dreamed of in Python 2, then, down the road, having to convert all my code to Python 3. I’ll still use Python 2 for learning and experimentation, for example, if a course requires it or a library I want to play with doesn’t work with 3.

Okay, now down to business. Here’s my latest progress report on The Adventure..

A while back I was all happy faced and giggly about having conjured up my very own graphical app. Never mind that it didn’t do anything; it was delightful eye candy to me compared to command-line based Python apps. I found that learning Tkinter took a lot of focus. Gone were the days of dragging, dropping and sizing graphical elements on a form like in Microsoft Access. With Tkinter, every box and button must be conjured up with code and programmatically assigned its place in the application window. I found this challenging at first. Just coming up with the pretty interface and making it not look weird required a lot of trial and error. Then I got distracted with my CSV-parsing project.

Now, finally, I am back on track thanks to Real Python’s chapter on simple Tkinter programming, as well as this Tkinter tutorial (note it’s almost TEN YEARS OLD but still works!), along with Tk Docs. I was finally able to get my cool app to perform the cool task of making people cool. Here it is, now in Python 3, with enough coolness to reverse global warming. Of course, unless you have the exact same friends as me, you’ll have to edit the list of cool people as needed.

# Apply Coolness 3.0
# Python 3

from tkinter import *

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

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

# Create the frame to hold the boxes & button
mainframe = Frame(window)

# We will be using grid() to place widgets in the frame, so..
mainframe.grid()

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

# Label that tells the user what to do.
lblPrompt = Label(mainframe, text="Enter the name of the person to be made cool:")
lblPrompt.grid(column=1, row=1, columnspan=2, padx=3, pady=3)

# Box in which user enters a person's name.
txtEntry = Entry(mainframe)
txtEntry.grid(column=1, row=2, padx=3, pady=3)

# The button that activates the coolness function.
btnCool = Button(mainframe,text="Make Cool!",command=coolness)
btnCool.grid(column=2, row=2, padx=3, pady=3)

lblResult = Label(mainframe, text="Result:")
lblResult.grid(column=1,row=3, sticky=W, padx=3, pady=3)

# Box that displays the result of the procedure.
txtResult = Label(mainframe, width = 20, height = 5, bd="3", relief=SUNKEN)
txtResult.grid(column=1, row=4, columnspan=2, sticky=(E,W))

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

Screenshot:
coolness-rob
Oops:
coolness-tony