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

22 August 2012

Learn Python on the Raspberry Pi: A GUI World

Part four of my six part Learn Python on the Raspberry Pi series introduces the Tkinter module, which allows us to code Graphical User Interface (GUI) apps, complete with windows and widgets.

The article shows how to create a file search app with search criteria entry fields, search button and scrolling results list.

Unfortunately, there's a missing boxout containing instructions on installing the Tkinter module. It's a simple enough task, just type the following line into a terminal window:
sudo apt-get install python-tk
If you don't have a Raspberry Pi you can still follow the series with these emulation instructions.

Here are a few extracts from Part 4:

Every GUI application has a very similar structure. Once we understand this structure we'll be able to create any number of GUI programs, from the very simple to the highly complex.

Just as we saw last time, we can use functions to divide the program into easy-to-read, easy-to-maintain code blocks. A typical GUI program has four main sections:
  • window and widget creation
  • window initialisation
  • the main window loop
  • event handlers

A GUI program must be able to determine which key was pressed. Not just the alphanumeric keys, but also shift keys, control keys, alt keys, function keys, cursor keys, the escape key and special keys like the Windows key. As for the mouse, a program needs to determine the current coordinates of the mouse pointer, whether a mouse button has been clicked and if the scroll wheel has moved.

How do we do this? In a GUI program it's done with something called 'callbacks'.

As you can see a GUI program requires quite a bit more code than a simple terminal-based one. However, the flexibility offered by a widget-based user interface is certainly worth the effort. And the possibilities for refinement are almost endless. For example, you could change a widget's size or colour, add new widgets, or create a completely different layout.

Next time I'll show you how to have loads of fun with turtle graphics.

Visit my Raspberry Pi page for news, reviews, advice and tutorials.

No comments: