Home » Python

GUI programming in Python using tkinter Module

In this tutorial, we will learn the basic concept of GUI (graphical user interface) programming in Python using tkinter module. The main focus of this tutorial to understand the basic concept of the tkinter module which helps us to create any GUI application in Python.
Submitted by Bipin Kumar, on October 19, 2019

GUI (Graphical User Interface):

GUI is a simple application which helps the user to interact with the computer or any other electronic device through a graphical icon. This used to perform different tasks on a desktop or laptop.

GUI tkinter module

tkinter is an inbuilt Python module used to create a GUI application. Python offers a lot of options for creating GUI out of which tkinter is most commonly used. You don't need to worry about installation because it comes with Python.

There is the most common way to create a GUI application using tkinter:

  • Import the tkinter module in the program.
  • Create the GUI application's main window.
  • Add any number of widgets to the GUI application's main window.
  • Apply the main event loop to widgets.

There are mainly two methods we have to remember during the creation of the GUI application by using tkinter module in Python.

1) tkinter.Tk()

To create the main window of the GUI application tkinter offers a Tk() function.

Syntax:

    Includehelp=tkinter.Tk()

Where, Includehelp is the name of the GUI application's main window.

2) mainloop()

This is used when you are ready for the application to run. This telling the code keep displaying the window until manually closed it.

Syntax:

    Includehelp.mainloop()

In tkinter, all widgets will have some geometry measurement and it has three mainly geometry manager classes which are discussed below.

  1. pack(): It organizes the widgets in blocks before placing them in the parent widget.
  2. grid(): It organizes the widgets in the grid before placing it in the parent widget.
  3. place(): It organizes the widgets by placing them on specific positions directed by us.

In tkinter, there is a lot of widgets provided to use in the GUI application. Some of these major widgets will be discussed below:

1) Button

To add a button in the GUI application, we are using this widget in the program.

Syntax:

    button_name=tkinter,Button(
            parents_window_name,  
            text='text_writing', 
            width='width_of_text', 
            command='function to call')

2) Radio Button

The widgets used to offer multiple options to the user. To add radio button we can simply use RadioButton class.

    rad1=Radiobutton(
        Parents_window_name, 
        text='text_writing', 
        value=numerical_value).pack()

We have to give a different value for every radio button, otherwise, they won't work.

3) Listbox

This widget offers a list of options to the user to select any options.

Syntax:

    List=Listbox(Parents_window_name)
    List.insert(1, text)
    List.insert(2, nexttext)

4) Entry

This widget provide user to enter multiple line text input.

Syntax:

    label1=Label(Parents_window_name, text='Name').grid(row=0)
    label2=Label(Parents_window_name, text='password').grid(row=1)
    entry_in_label1 = Entry(Parents_window_name)
    entry_in_label2 = Entry(Parents_window_name)
    entry_in_label1.grid(row=0, column=1)
    entry_in_label2.grid(row=1, column=1)

The implementation of these methods is in the below program.

Program:

import tkinter
from tkinter import *

Includehelp.title('Includehelp')

rad1=Radiobutton(Includehelp, text='Content', value=1).pack()
rad2=Radiobutton(Includehelp, text='Software', value=2).pack()
lebel=tkinter.Button(Includehelp,text='Welcome to Includehelp', width=100).pack() 

Includehelp.mainloop()
tkinter module example 1







Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.