/* jquery */ /* jquery accordion style*/ /* jquery init */

23 April 2013

Learn Python - Basic GUI App

Now Tkinter is installed it's time to write a GUI app.

Boot up your Raspberry Pi and start the Geany editor. Next open a new file and save it as tk-basic.py in the Desktop's Python folder.

Now type in the code below:

# A basic GUI program using TK
# Created by David Briddock

from Tkinter import *
import tkMessageBox

# initialise main window
def init(win):
  win.title("Basic GUI Program")
  win.minsize(200, 50)

# find button callback
def hello():
  tkMessageBox.showinfo("Hello", "The callback worked!")

# create top-level window object
win = Tk()

# create a widget
btn = Button(win, text="Hello", command=hello)

# initialise and start main loop

Before we run this app let's examine the code in detail.

Basic Flow

After the program-level comments there's a Tkinter module import statement on line 4. However, you'll notice it's a little different to the ones we've used before.

What this style of import does is alleviate the need to prefix all the Tkinter's modules constants and functions. We'll need quite a few Tkinter constants and functions for any reasonably sized GUI program, so this import option will help de-clutter the code - and save quite a bit of repetitive typing.

There's a more typical style import on line 5 for the Tkinter-specific tkMessageBox module.

In order follow the program execution flow we need to skip past the function definitions and look further down the listing.

Window And Widget Creation

At line 18 we create a top-level window object and assign it to the variable win.

On line 21 we have the one and only widget, a button. When we create a widget we need to say where it fits in the window hierarchy. This is done by specifying the parent window as the first parameter. Widget characteristics are set by defining one or more optional parameters. Here we have specified two.

The text parameter will set the button's text label. The command parameter defines the callback function name. When a mouse click event occurs for this button, the named function is called, in this case hello.

Initialise And Loop

Lines 24 and 25 hold the key to the program's operation. Line 24 will call the init function to perform any initialisation operations. We'll look at this shortly.

Line 25 is the main GUI loop. It's an endless loop which only terminates when the window is closed. Entering this loop will cause the main window to be displayed, along with any widgets we've defined. Once running it will capture and process all keyboard and mouse events. If these events are associated with any widgets, such as our button, the appropriate callback function will be called.

Initialisation Function

As we saw earlier, line 24 calls the init function. In this basic program init contains just three lines.

On line 9 we set the top-level window title, which will appear in the titlebar. On the next line we specify the minimum size for our window in pixels. In this case it's 200 pixels wide and 100 pixels high.

The final function statement, on line 11, calls the Tkinter pack function on behalf of our button. This adds the button to its parent window, namely the top-level window win.

The Callback Function

The callback function, hello, is defined on line 14. Remember, callback functions never have any parameters.

Once again this is a small function with just a single line of code. Line 15 calls the tkMessageBox module function showinfo. This function creates a new popup-style window, and you'll notice it has two parameters. The first parameter will define the string in the window's titlebar. While the second parameter is the message that will appear inside the popup window.

Run The App

Now we can execute the app, using the Geany Build->Execute menu option or the F5 key. After the window appears click on the button to see the message window popup.

Because the callback is only associated with the button, if you click somewhere else inside the top-level window nothing happens.

A post from my Learn Python on the Raspberry Pi tutorial.