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.
Executing these two lines will cause the following simple windows to appear.
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.
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”.
Executing this code starts the following GUI Window:
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.
Executing this script will open the following window:
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.
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.
Executing this script starts the following window:
Clicking the Close button closes the window.
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.
Executing this script open the following window, with the listbox items added:
To enable multiple selection, use the option selectmode=’multiple’, as follows:
LB1 = tkinter.Listbox(MainWindow,width =20,selectmode=’multiple’)
The SpinBox control uses arrows to change a value in a pre-specified range.
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:
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.
Executing the above script will open the following window:
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.
Executing this script opens the following window:
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.
Executing the script start the following window:
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).
Executing the above script starts the following window:
The Entry tool allows the user to enter a single line of text.
Executing the above script will start the following window:
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.
While the following line prompts the user whether he wants to continue, providing him with Yes, No, and Cancel options.
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.
Now, let’s execute it:
Type your first name and last name, and then press “Greeting”.
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.