Python tkinter Examples

Kailash Chandra Behera | Friday, July 31, 2020

Introduction

This blog provides python Tkinter example to build python GUI using python widgets. Here we will see code example how to use tkinter windows widget in python programming.

Getting Started

The Tkinter is the most commonly used method to build GUI in python although Python offers multiple options for developing GUI. It is a standard Python interface to the python Tk GUI toolkit shipped with Python and is available on most Unix platforms, as well as on Windows systems. Tkinter is faster and easiest wat to create the GUI application in Python.

Python GUI Creation

To build a GUI application in python need to perform the below steps.

  1. Import the Tkinter module.
  2. Create main window of python gui.
  3. Add widgets to the GUI application.
  4. Invoke the mainloop method of main window.

Importing Tkinter is not much difficult, importing Tkinter is the same as importing any other module in the python code. But there is a bit different in name of Tkinter python 2 and Tkinter python 3. To import Tkinter in Python, the below small code is used.

 import tkinter  

Tkinter offers two methods to Tkinter GUI or displays a window control in Python, these methods are Tk() and mainloop(). The Tk() method is two types that are without parameter and with parameter. The parameter Tk() method is loos like below.

 Tk(screenName=None, baseName=None, className=’Tk’, useTK=1)  

Python Tkinter Examples

The name mainloop() method is used when you are ready for the application to run. mainloop() is an infinite loop used to run the application, wait for an event to occur and process the event till the window is not closed.

Python GUI Example -1

 import tkinter   
 m = tkinter.Tk()   
 m.mainloop()  

tkinter windows

Python GUI Example -2

 import tkinter   
 m = tkinter.Tk(screenName='Kailash', baseName='GK', className=’Tk’, useTk=1)   
 m.mainloop()  

python window

Summary

Here in the above, we build python hello world using python programming where we created python GUI and how to show main window in different ways. I hope you have enjoyed it a lot.

Thanks