Python GUI Programming (Tkinter) – Part I

In this Python GUI programming tutorial you will learn about how to make GUI programs using Python Tkinter toolkit.

Graphical User Interface, also known as GUI is one of the best features in programming that makes a program look visually more attractive than the normal text based programs covered with a black or any other static color. GUI makes user interactions much easier and consistent.

 

Python GUI Programming

To create a GUI program in Python, you will need to use a GUI toolkit. One of the widely used toolkit available in Python is Tkinter. It is one of the most stable and popular GUI toolkit that Python developers use.

 

Install Tkinter Module

Tkinter module is by default available in Windows operating system. However, if you’re using any other operating system, you will need to download the module separately.

 

For Windows OS
https://www.python.org/downloads/

 

For Linux OS

sudo apt-get install python-tk (For Python 2.x)
sudo apt-get install python3-tk (For Python 3.x)

 

You can create GUI elements by instantiating objects from classes pre-defined in Tkinter module, included in the Tkinter Toolkit.

 

Some of the GUI Elements in Tkinter module are listed below.

 

Frame: It holds other GUI elements such as Label, Button, TextBox ,etc.

Label: It displays an uneditable text or icons on the screen layout.

Button: It performs an action when the user activates it or presses it using mouse.

Text Entry: It accepts one line of text and displays it.

Text Box: This GUI element accepts multiple lines of text and displays it.

Check Button: It allows the user to select or unselect an option.

Radio Button: This button provides the user to select one option from several options listed.

 

GUI programs are traditionally event driven. Event driven means that the Buttons, Icons or any other graphical object on the screen respond to actions regardless of the order in which they occur. They respond to the actions performed by the user and not the logical flow as we saw in text based programming before.

 

Python GUI Programming Example

 

Output

Python GUI Programming (Tkinter) - Part I 1

 

The first line of the program code imports the Tkinter module from the Python library in the namesapce of the current program.

The second statement is used to instantiate an object of the Tkinter class Tk and it is assigned to a user defined variable root.

The third statement uses the title() method that describes the title to be displayed on to the window title bar. It takes in a string argument.

The third statement uses a geometry() method which is used to set the dimensions of the window. It takes in a string (and not integers) as arguments that
represents the window’s width and height, separated by the “x” character.

Next statement finally starts up the GUI window application and waits in for the user to command in.

Note: You can only have one root window in a Tkinter program. If you try to create multiple windows in one single program, you program is bound to crash.

 

Python Tkinter Frame

A Frame is a widget or a base widget which is used to place in other widget such as labels, text boxes and others. This are an important first step as nothing can be done unless frames are developed. It basically holds other widgets.

 

app=Frame(root)

Here, we have passed root to the Frame constructor. As a result, the new frame is placed inside the root window.

 

app.grid()

grid() method is the one that all widgets have. It’s associated with a layout manager, which lets you arrange widgets in a Frame.

 

Python Tkinter Label

GUI elements are called widgets. Label is one of the simplest widget. It consists of uneditable text or icons (or both). It’s often used to label other widgets. Labels aren’t interactive. You won’t generate any command by clicking on a label. But, labels are important for naming other widgets so that there is no confusion for the end user.

 

Example

 

Output

Python GUI Programming (Tkinter) - Part I 2

 

Here, we have first created a Frame which is assigned to variable frame1. We have then passed frame1 to the label1 object’s constructor, and thereby the frame that app refers to the master of the label widget. As a result, the label is placed in the frame.

 

In this next Python GUI Programming tutorial we will discuss about more GUI elements or widgets.

Leave a Comment

Your email address will not be published. Required fields are marked *