87 / 100

Python Tkinter Tutorial

In this tutorial, we will learn How to use Tkinter for GUI of any application.

Python Tkinter Tutorial provides many libraries for GUI development. Out of those, Tkinter is the most widely used library. You don’t need to install this library separately, it comes embedded with a python interpreter. So when you install python it automatically gets installed.

Python Tkinter Tutorial
Python Tkinter Tutorial

How to start with Python Tkinter Tutorial:

First of all, if you are using any other interpreter rather than Python then you need to check whether Tkinter is installed or not.

import tkinter

import _tkinter

tkinter._test()

If all this runs successfully then it is ready to use otherwise you have to install it first.

Run the following code:

from tkinter import *

from tkinter import ttk

root=Tk()

root.mainloop()

Description:

The first line imports the Tkinter library for our application. Second-line import the ttk module for using themed widget sets. The third line creates a root or parent window which will contain all other widgets needed for the application. The fourth line calls the method ‘mainloop()’ which will run the application and it will run infinitely.

Why do we import ttk separately?

By seeing the code one can think although we have imported all the modules using “*”, why are we importing ttk separately? Because when you see it’s code for init.py, you will notice that ttk will not get imported to your code by wildcard import. So this is the reason we have to import ttk explicitly for using it in our code.

Note: You can also use the widget of Tkinter but ttk offers more widgets with improved features. So it’s better to use widgets of the ttk module.

Managing Geometry in Tkinter:

There are three methods available for managing geometry in tkinter.

  1. Pack Geometry Manager
  2. Place Geometry Manager
  3. Grid Geometry Manager

Pack Geometry Manager:

By using a pack geometry manager, we have to specify the position of widgets by using alignments such as left, right, top and bottom, etc.

Place Geometry Manager:

In this geometry manager position of widgets is specified by specifying its absolute position in pixels as we in HTML.

Grid Geometry Manager:

In this geometry manager, the whole window get divided in the form of a 2D array and the position of each widget is specified by its corresponding row and column.

Event Handling in Python Tkinter Tutorial:

If we are developing a GUI then we also need to handle its corresponding events. So there are multiple events are for each widget.

Example:

When Event OccursEvent Handler Code
<ButtonPress>handle_buttonpress()
<Key>handle_key()
<Leave>handle_leave()
<Motion>handle_motion()
<Configure>handle_configure()
Event Handling in Tkinter

Some commonly used Widgets:

1.     Label

The label is a Tkinter widget and it is also available in the ttk module. We can use labels for specifying some textual information or we can also insert images in a label.

label=ttk.Label(root,text=’ I am a Label’) # root decides the parent window in which it will be placed

label.pack() #by using pack we are calling it on its parent window that is the root

2.     Button

The button is one of the widely used widgets. We can use it for firing some events on its click event.

button=ttk.Button(root,text=’Click Me’)# root decides the parent window in which it will be placed

button.pack() #by using pack we are calling it on its parent window that is the root

3.     Radio Button

We can use radio button for selecting a single value from multiple choices available. for using a radio button first of all we have to create a StringVar() that will be used getting and setting the value of radion button

city=StringVar()

tk.RadioButton(root,variable=city,value=’Lucknow’).pack()

tk.RadioButton(root,variable=city,value=’Delhi’).pack()

tk.RadioButton(root,variable=city,value=’Noida’).pack()

tk.RadioButton(root,variable=city,value=’Pune’’).pack()

city.get() # it will output the value of selected choice

4.     CheckButton (CheckBox)

CheckButton is the same as the checkbox available in HTML.

check_button1=ttk.CheckButton(root,text=’Location 1’)

check_button2=ttk.CheckButton(root,text=’Location 2’)

check_butoon1.pack()

check_button2.pack()

5.     ComboBox

ComboBox is used for creating a dropdown list and by using this we can select one or multiple values.

month=StrngVar()

combobox=ttk.ComboBox(root,textvariable=month)

combobox.pack()

combobox.config(value=(‘Jan’,’Feb’,’Mar’,’Apr’,’Jun’,’July’))

month.get() #display the selected value from combobox

6.     Entry

This widget can be used for getting a single line input from user such as comments, suggestions, etc.

entry=ttk.Entry(root,width=30)

entry.pack()

7.     Frame

Frames are one of the most useful widgets because these are helpful in geometry management.

frame=ttk.Frame(root)

frame.pack() #default frame size is zero

frame.config(width=100,height=50) # these are given in pixels

So these are some commonly used widgets in tkinter. Rather than these,  there are multiple widgets available for use such as PanedWindow, Top-Level Window, Text, SpinBox, ProgressBar, etc. For More Information follow this page Python Tkinter Tutorial and for Python Tutorial Follow this page Python Tutorial.

Video Tutorial for Python Tkinter

6 thoughts on “Free Python Tkinter Tutorial for best GUI”

  1. I am now not positive where you’re getting your information,
    but good topic. I needs to spend a while studying much more or
    working out more. Thanks for great information I was searching
    for this information for my mission.

    Reply

Leave a Comment

Share via
Copy link
Powered by Social Snap