< Prev  |  Home  |  Next >


As you know, the ‘I’ in GUI stands for ‘Interface’. So far, we’ve created lovely GUIs, but they don’t really do anything. The whole point of a GUI is that it provides a nice, easy to use interface to a Python program. In this section, you’ll learn how to make a GUI more useful.

Here’s what happens. In this example, the user clicked the button 3 times:


The important bit here is the argument “command=callback” when creating the button. This is the name of the callback function that is called when the button is clicked. Printing something in the Python shell isn’t really in the spirit of a GUI, so let’s make the message appear in a label:

Instead of printing to the shell window, this program just configures the label by changing the text displayed. Here’s what the label looks like before and after the button has been clicked:

A callback to update a label

We could also use variables in our programs, like this program to count the number of times a button has been clicked:

There isn’t really anything here that you haven’t seen before, except for the use of theĀ global keyword:

This just tells the function that it should use the variable called ‘presses’ that has been declared globally (i.e. outside of the function itself).

A program to count button presses

Did I really click that button 4239 times? No? Then how did I make that printscreen?


Develop a program to change the background colour of a window by clicking one of 2 buttons. For example the following window can be changed to blue of red by clicking the buttons. I have defined 2 separate functions called ‘makeBlue()’ and ‘makeRed()’.

Changing the window colour with buttons

< Prev  |  Home  |  Next >