Python GUI Programming using Tkinter

Introduction

Like other programming languages, Python also supports GUI (Graphic User Interface). Python introduces Tkinter lib as python GUI lib for making a GUI in python. Tkinter is a standard GUI library for Python, hence the python GUI is also called Tkinter GUI.

Here we will discuss a little about the widgets provided by Tkinter lib and will see how to build GUI in python.

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

The blog provided code examples to create simple python gui programming which displays a Windows control in two different ways. 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()  

GUI using Python

Python GUI Example -2

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

GUI using Python

Python Widgets

Widgets are standard graphical user interface (GUI) elements and nothing but controls like button, canvas, grid, etc. which we are using in Windows application in .Net Technology. Python provides various widgets like other technology for windows applications.

Here in this article, we will discuss some main widgets which are frequently used in the Python GUI programming.

  1. Button: The Button widget is used to add buttons in a Python application. These buttons can display text or images that convey the purpose of the buttons. You can attach a function or a method to a button which is called automatically when you click the button.
  2. Canvas: It is used to draw pictures and other complex layout like graphics, text and widgets.
  3. Check Button: To select any number of options by displaying a number of options to a user as toggle buttons.
  4. Entry: It is used to accept inputs text from user the single line text entry from the user.
  5. Text: It is similar like Entry, but it accepts multiple lines.
  6. Label : A Label widget shows text to the user. You can update the widget programmatically too.
  7. Frame :It acts as a container to hold the widgets. It is used for grouping and organizing the widgets.
  8. List box: It offers a list to the user from which the user can accept any number of options.
  9. Message: The widget can be used to display short text messages. The message widget is similar in its functionality to the Label widget, but it is more flexible in displaying text.
  10. Radio Button: It is used to offer multi-choice option to the user. It offers several options to the user and the user has to choose one option.
  11. Scale: It is nothing but slider that allows to select any value from that scale.
  12. Scrollbar: It refers to the slide controller which will be used to implement listed widgets.
  13. Panned Window: It is a container widget which is used to handle number of panes arranged in it.
  14. Spin Box: It is an entry of ‘Entry’ widget. Here, value can be input by selecting a fixed value of numbers.

Summary

In the above, we say how to buil gui using python, how to use gui application in python and the various commonly python widgets. I hope you have enjoyed it a lot.

Thanks

Kailash Chandra Behera

An IT Professional with 12 years experience in development life cycle in windows, service and Web based application using Microsoft.Net technologies. Proven record of developing all phases of projects in Microsoft.Net technology from initiation to closure aligning with the company's Business objectives to drive process improvements, competitive advantage and bottom-line gains. -> Good exposure of independently working and developing multiple projects ->Committed to efficient and effective development of projects in a fast-paced and deadline driver environment. Skill :- Develop and design projects in various technologies of Microsoft Technology. Total IT Experience- 13+

Previous Post Next Post

نموذج الاتصال