Learn the basics of GUI Programming in Python

5
51775

Python Tutorial – Advanced Topics

One of the best features of Python as a programming language is its support for developing Graphical User Interfaces GUI Applications. In this article, we are going to learn how to develop basic GUI applications using Python. We will use the Tkinter module which acts as an interface between Python and the underlying Tk GUI toolkit.

What is Tkinter?
Tkinter is Python’s de-facto standard (Graphical User Interface) package.

First Program
1
Executing these two lines will cause the following simple windows to appear.
2
As you see, importing the tkinter module (package) is a necessity to be able to use its classes and methods. The Tk class when instantiated creates a top-level (main) window.

Using Controls
Despite being a GUI program, it is a trivial one. Nobody will be interested in an empty window application. So, what?! A rich list of controls exists to allow the developer to use and customize his GUI program. Such list includes (but are not limited to) Labels, Buttons, List Boxes, Menus, Menu Buttons, etc.
The following is an example for a GUI Application with one main window that contains a label saying “Hello, I am a Python Programmer”.
3
Executing this code starts the following GUI Window:
4
In this script, we started by creating an object MainWindow of the Tk class. This creates the Window (as in the first program). Next, we have created an instance L1 of the Label class. The Label object was given two arguments:

  • The Master: which is the container that will host the Label.
  • The Text: that will be displayed in the label.

The pack method is then called to organize the geometry of the control (label) within its master.

Customizing the Label
The type, size, and heaviness of the font used to display text in label controls can be customized. Also, the font color and background color can be set as per your preference.
In the following script, I am going to write my favorite Gandhi quote in bold red, using the Arial font with size 10, on a yellow background.
5
Executing this script will open the following window:
6
In the Label object definition step, the option bg (background color), fg (forground), and font have been set to give the custom view above.

Setting the Title of the Main Window
To change the text in the title bar of the application main window, the title method could be used.
MainWindow.title(“Gandhi Quotes”)
7
Using Buttons
GUI programs wait for user actions. One of the most common actions is pressing buttons. The following script uses a button that when clicked closes (destroys) the application window.
8
Executing this script starts the following window:
9
Clicking the Close button closes the window.

Ultimate Java Development and Certification Course
Using ListBoxes
The List Box is a control that allows single and multiple selections between various items. The Following script defines a listbox and reads country names from an array, and then inserts these names into the listbox.
10
Executing this script open the following window, with the listbox items added:
11
To enable multiple selection, use the option selectmode=’multiple’, as follows:
LB1 = tkinter.Listbox(MainWindow,width =20,selectmode=’multiple’)
12
Using SpinBox
The SpinBox control uses arrows to change a value in a pre-specified range.
13
In this script, an instance of the Spinbox class is declared, and assigned values from 1 to 12.
Executing the script opens the following window:
14
The SpinBox can also take its available items from a tuple of values. In the following example, I am going to define a tuple days that contain the names of the weekdays. The SpinBox will be configured to have these names as item values.
15
Executing the above script will open the following window:
16
Scale (Slide Bar)
This control allows the user to graphically choose a value from scale by sliding the bar.
The following script creates a horizontal slide bar that ranges from 0 to 100.
17
Executing this script opens the following window:
18

C Programming_1
Using Combobox
The combobox is a dropdown list that provides a graphical way for the user to select one value of the list. To use the Combobox control, we need to import the tkinter.ttk module.
The following script creates a combobox that contains the names of the weekdays.
19
Executing the script start the following window:
20
Radio Buttons
Radio Buttons provides a way to select one (and only one) option from few (may be two, three, etc.) number of available options. The following script creates radio buttons that allow the user to choose gender (male or female).
21
Executing the above script starts the following window:
22
Entry
The Entry tool allows the user to enter a single line of text.
23
Executing the above script will start the following window:
24
MessageBox
Message Boxes are very common tools in GUI applications. They provide the application with means to communicate with the user, by sending him a message and get his response.
The tkinter.messagebox can be used to display various types of message boxes.
For example: the following line of code displays a message box that asks the user either to press OK or Cancel.
25
While the following line prompts the user whether he wants to continue, providing him with Yes, No, and Cancel options.
26
Example
In this example, we are going to write a script that opens a window having two entry boxes: one labeled Firstname, and the other Lastname. The window has two buttons: “Greeting” that displays a welcome message box to the user, and “Close” that closes the application.
27
Now, let’s execute it:
28
Type your first name and last name, and then press “Greeting”.
29
Pressing “Close” will terminate the application.

* * * * * *

In this article, we have learned the very basics of GUI programming in Python. We have seen that the Tkinter module contains most of the required classes and methods to create nice GUI applications. We have used the Tk class to create a master (main window), and instantiated objects to insert various controls on the application window. We have seen Labels, ComboBoxes, Radio Buttons, Slider Bars, and Message Boxes.

I hope this article serves as a good introduction to the GUI programming topic, which is really a deep one that can never be covered by one article.

Good Luck.

5 COMMENTS

  1. Really great post you gave me here. Just remember how it was complicated to develop windows gui applications 15 years ago and how it is so simple using Python !!!
    I’ll sure google for some advanced tutorials.

  2. Aren’t these examples supposed to have a “mainloop()” call? Without this, the GUI window won’t stay created long enough for you to see it.

LEAVE A REPLY

Please enter your comment!
Please enter your name here