Python GTK - Widget, Packing, Event and Feedback example
Archive - Originally posted on "The Horse's Mouth" - 2007-04-09 15:31:12 - Graham EllisPython has a variety of GUIs available ... one of which, GTK, has been getting much more popular of late. As is common with many of the GUIs in Pyton and other languages, you'll typically write an application as follows:
- Initial code, defining
- Widgets
- Layout (Geometry)
- Events
- A main loop, which collects actions and events
Within the GUI, each widget must be defined, packed into the appropriate piece of Geometry, and shown (and the box and window containers also need to be shown) and - there you are - a simple GUI.
As you use the GUI, you'll want to provide your user with feedback by altering the text on labels, buttons, and making other widget changes. Slightly surprisingly, you define all the possible actions early on then have your events trigger them as callback functions
The authors of GUI documentation always seem to start with a very very simple "hello world" example, then leap off into some quite extravagant further example, when all you (the programmer) want is a few buttons to perform tasks (and change to show what you've done). Of course, I do know why they make them complicated as they've got dozens of events, scores of widgets and hundreds of facilities they would love you to use.
But I'm going to break from convention today and point you in the direction of a simple Python Gtk example that starts like this:

As you run the GUI, you'll see the display counting up the number of times each button is pressed:

Complete (commented!) source code is here where you'll find further examples as well.