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.

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

1

2

3

4

5

fromTkinterimport*

root=Tk()

root.title("My First GUI")

root.geometry("500x500")

root.mainloop()

Output

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

1

2

3

4

5

6

7

8

9

fromTkinterimport*

root=Tk()

root.title("My First GUI")

root.geometry("800x200")

frame1=Frame(root)

frame1.grid()

label1=Label(frame1,text="Here is a label!")

label1.grid()

root.mainloop()

Output

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.