Tkinter Module in Python

The Tkinter package (“Tk interface”) is the standard Python interface to the Tk GUI toolkit. Both Tk and Tkinter are available on most Unix platforms, as well as on Windows systems. It helps in making the GUI based window for any application.

In this tutorial will look at some of the general GUI using Tkinter. It comes already installed in Python. For the whole series, I used Python3. It also works in Python2 but there is a difference in capitalization. For Python2 it is-:

import Tkinter

Procedure for creating a GUI using Tkinter:

  1. Import the module Tkinter.
  2. Have to create a tk object -> This creates an atop-level widget of Tk which usually is the main window of an application.
  3. Create a window class which is defined by all the functions you want to create in that GUI.
  4. mainloop() -> It will keep your GUI alive until you perform any event.

Let’s look with the help of examples:

A simple demo of how to create a GUI based window using Tkinter

The output of the above code is shown below. In this, it will show the window GUI generated by the Tkinter module.

There are geometric managers in this module.

  1. place -> It will place your widget at the mentioned coordinates.
  2. pack() -> It organizes the widgets in blocks before placing in the parent widget.
  3. grid() -> It organizes the widgets in grid (table-like structure) before placing in the parent widget.

Creating widgets


Adding buttons in the window.

The output of the above code is shown below and If you click quit button it will quit the window. If you will click the hello button then it will print the message.

As we saw in ide like File menu, Edit menu etc.

The output of the above program is shown below i.e, it will create a menu bar and if you will click File menu then save and open option will pop up. Right now the functionality is just printing a message but you can modify the code in order to make changes in functionality.

Close Menu