Using the Tkinter
User Interface: A Quick Introduction
What is Tkinter?
Tkinter is the Python interface to the Tcl/Tk GUI toolkit (hence the name Tkinter)
The TclTk GUI toolkit works across a number of
windowing systems (Mac, Windows, Linux, etc)
Tkinter is pronounced a whole bunch of ways:
T-K-inter T�kinter, and others (seems like most
call it T-K-inter)
Tkinter is included in Python distributions (like Anaconda)
and is very popular
It provides Python developers with a relatively easy handle to the
graphical user interface (GUI) of the native operating system (Linux, Mac OS,
Windows, etc)
You can use Tkinter to draw, browse for files,
and do just about everything you see in a modern GUI application
This code (from http://effbot.org/tkinterbook/canvas.htm)
creates a window, adds a canvas, and draws on it
This code (from http://tkinter.unpythonic.net/wiki/tkFileDialog)
creates a window and populates it with buttons that interact with the file
system
As you can see from these 2 samples, you can approach Tkinter
in very different ways
Designing and building programs for
an event driven environment
After setup, your program doesn�t �do� anything until it gets an event
(like a button push, etc)
Actions in your program are initiated by user requests
The design of the program starts with a good user interface
A crash course in Tkinter and event driven processing
GUIs use an event handling model to respond to user and OS-level actions
Types of events include painting requests, mouse clicks or drags, text
entry, and so on
An event driven program spends most of its time in a message loop that
waits for the OS to send it event notifications (Hey�you got a mouse click) and
related details (the mouse click happened at x=348, y=212)
The message loop sends the notification to the appropriate handler in
your program
Let�s look at a relatively small program
that adds 1 button to a frame and interacts with it
This example comes from http://effbot.org/tkinterbook/button.htm
Clicking on the button causes Click! to be
printed in the console window
The program, line by line:
# Import the Tkinter module:
from Tkinter import *
# Create the main window (master):
master = Tk()
# Create an event handler (a �callback� function)
# called callback() to handle mouse clicks:
def callback():
print "click!"
# Construct an object b of the Button class that will be
# placed on master, is labeled OK, and is associated
# with the event handler callback()
b = Button(master, text="OK",
command=callback)
# run the layout manager to actually place the button on master
b.pack()
# Start the message handling loop
mainloop()
Some notes on names and conventions:
You don�t have to name a callback function callback()�you
can call it anything you want
Although master is a conventional name for a main window, root is also
used frequently
Be conventional, however; use a name that people will recognize
Here is an extension of the previous example
with 2 buttons
There are now 2 handlers (callbacks); one for each button
This is a simple working program for
calculating the earth distance along a line of latitude
It�s functionally identical to the
Java program described here but uses better exception handling