Tkinter module in Python

Hello Friends,

Today in this article we are going to discuss about some basics of Tkinter module in Python In this series we will cover only Tkinter.

Take away from this article

  • What is Tkinter?
  • We will get to know about different widgets in Tkinter
  • All the widgets demos with comments

What is Tkinter?

The Tkinter is a Graphics User Interface (GUI) toolkit. It is mostly used to create apps or user forms.

Some other Python Libraries available for creating our own GUI applications are Kivy Python Qt wxPython

  • Kivy
  • Python Qt
  • wxPython

Among all Tkinter is most widely used

We don’t need to download Tkinter because it is in built-in

Basic Tkinter Widgets:

WidgetsDescription
LabelIt is used to display text or image on the screen
ButtonIt is used to add buttons to your application
CanvasIt is used to draw pictures and others layouts like texts, graphics etc.
ComboBoxIt contains a down arrow to select from list of available options
CheckButtonIt displays a number of options to the user as toggle buttons from which user can select any number of options.
EntryIt is used to input single line text entry from user
FrameIt is used as container to hold and organize the widgets
MessageIt works same as that of label and refers to multi-line and non-editable text
ScaleIt is used to provide a graphical slider which allows to select any value from that scale
SpinBoxIt is allows user to select from given set of values
TextIt allows user to edit multiline text and format the way it has to be displayed
MenuIt is used to create all kinds of menu used by an application
ListUsed to display a list of options for the user to select.
Tkinter widgets

Now let’s start with the Tkinter


# Importing Tkinter 

from tkinter import *

# Writing code needs to create a main window
# Main window named window

windows = Tk()

# calling mainloop method which is used
# when your application is ready to run
# and it tells the code to keep displaying 

windows.mainloop()

Output –

A basic GUI screen will be created

Python - Tkinter - A basic GUI screen
Python – Tkinter – A basic GUI screen

Now will change the title and add some text.

from tkinter import *

windows = Tk()
Giving the title to main windows 
windows.title("My First GUI")

# Label is what output will be 
# show on the window

label = Label(windows,text="Hello World!")
# If we run this program the text will not be displayed
# To display the text we need to pack them 

label.pack()
windows.mainloop()

Output –

Python - Tkinter - GUI screen with title and label
Python – Tkinter – GUI screen with title and label

As we can see that the text and the title is displayed

Now we will create a button widget

# Creating a button

from tkinter import *
windows = Tk()
windows.title("My First GUI")

# To create button we will use button widget and give the text which # will display on the button
# Then we will store in variable and pack it

button1 = Button(text="Click")
button1.pack()

windows.mainloop()

Output –

Python - Tkinter - GUI button
Python – Tkinter – GUI button

We can also click this button but nothing will happen if we want to print something after clicking the button will use parameter command

from tkinter import *

# defining a function 

def hello():
    print("Hello World")

windows = Tk()
windows.title("My First GUI")

# Using command and calling function

button1 = Button(text="Click",command=hello)
button1.pack()

windows.mainloop()

Output –

Python - Tkinter - After clicking it prints Hello world
Python – Tkinter – After clicking it prints Hello world

Creating Canvas

In canvas will create oval and line

from tkinter import *

windows = Tk()
windows.title("My First GUI")

# Creating a canva
canva = Canvas(windows, background="yellow", height=223, width=223)


# Drawing a Line
line = canva.create_line(100,100,50,50,fill="green")


# Drawing a oval
oval = canva.create_oval(150,100,50,80)

canva.pack()
windows.mainloop()

Output –

Python - Tkinter - Basic Canva - a line and an oval
Python – Tkinter – Basic Canva – a line and an oval

Creating the Combobox

In the combobox will add some languages

import tkinter 

# Importing ttk module 
# Which is modern than tk and having more styles

from tkinter import ttk


windows = tkinter.Tk()
windows.title("My First GUI")


# Creating the combobox
# we can take the user input by creating an instance of the 
# StringVar() object

combobox = tkinter.StringVar()
languagechoosen = ttk.Combobox(windows, width = 27, textvariable = combobox)


# Adding the list of languages in combobox

languagechoosen["values"] = (
    "Python",
    "C",
    "C++",
    "Java",
)
languagechoosen.pack()
windows.mainloop()

Output –

Python - Tkinter - simple combobox
Python – Tkinter – simple combobox

Creating Check buttons

We will create multiple check button’s

from tkinter import*


windows = Tk()
windows.title("My First GUI")

# First Check button using checkbutton widget
checkbutton = Checkbutton(text="Python")
checkbutton.pack()

# second
checkbutton1 = Checkbutton(text="Java")
checkbutton1.pack()

# third
checkbutton2 = Checkbutton(text="Javascript")
checkbutton2.pack()

# fourth
checkbutton3 = Checkbutton(text="Go")
checkbutton3.pack()

Output –

Python - Tkinter -checkbox's
Python – Tkinter -checkbox’s

Creating Entry

In this widget we can take input from user it’s like a text box

from tkinter import*

windows = Tk()
windows.title("My First GUI")

# Creating a label
Label1 = Label(windows, text="User Name")
Label1.pack( side = TOP)

# Entry using Entry widget
entry1 = Entry(windows, bd =5)
entry1.pack(side=TOP)

windows.mainloop()

Output –

Python - Tkinter - Entry
Python – Tkinter – Entry

Creating Frame

from tkinter import *

windows = Tk()

# Creating frame using Frame widget
frame = Frame(windows,height=100,width=500,background="grey")
frame.pack()

# Giving label
label = Label(windows,text="Choose one option")
label.pack()

Creating buttons to look more attractive 
button = Button(windows, text="OK", width="20", height="5",background="black",fg="white")

button1 = Button(windows, text="Cancel", width="20", height="5",background="black",fg="white")

button.pack()
button1.pack()

windows.mainloop()

Output –

Python - Tkinter - Frame
Python – Tkinter – Frame

Creating Message

from tkinter import *

windows = Tk()


# Creating label
label = Label(text="Knowledge - Junction",font=90)
label.pack()

# Creating Message widget
msg = Message(windows,text = "Hello World\n Python")
msg.pack()

windows.mainloop()

Output –

Python - Tkinter - Message
Python – Tkinter – Message

Creating Scale

from tkinter import *

windows = Tk()


# It takes the float value
slide = DoubleVar()

# Creating Scale using Scale widget
scale1 = Scale(windows,variable='slide1', from_=0, to= 100, orient = "horizontal")
scale1.pack()

windows.mainloop()

Output –

Python – Tkinter – how the scale works

This video clip shows that how the scale works. We can also add vertical scale by changing the orient


Creating Spinbox

from tkinter import *

windows = Tk()


# Creating a label
label = Label(windows, text ='Knowledge - Junction', font = "50") 
label.pack()

# Creating a spinbox using Spinbox widget
spin = Spinbox(windows,from_=0,to=10)
spin.pack()

windows.mainloop()

Output –

Python - Tkinter - Spinbox
Python – Tkinter – Spinbox

Creating Text

from tkinter import *

windows = Tk()

# Creating text widget
text1 = Text(windows)
text1.insert(END,"Hello Freinds\nWelcome to Knowledge - Junction")
text1.pack()

windows.mainloop()

Output –

Python - Tkinter - Text
Python – Tkinter – Text

The user also can edit in Text widget

Creating Menu

import tkinter as tk
from tkinter import Menu


windows = tk.Tk()
windows.title('Menu Demo')

# create a menubar
menubar = Menu(windows)
windows.config(menu=menubar)

# create a menu
file_menu = Menu(menubar)

# add a menu item to the menu
file_menu.add_command(
    label='Exit',
    command=windows.quit
)


# cascade add the File menu to the menubar
menubar.add_cascade(
    label="File",
    menu=file_menu
)

windows.mainloop()

Output –

Python - Tkinter - Menu
Python – Tkinter – Menu

Creating Listbox

from tkinter import*

windows = Tk()


# Creating listbox
listbox = Listbox(windows, height = 10, 
                  width = 15, 
                  bg = "grey",
                  activestyle = 'dotbox', 
                  font = "Helvetica",
                  fg = "yellow")

# Inserting list
listbox.insert(1, "Python")
listbox.insert(2, "Java")
listbox.insert(3, "Javascript")
listbox.insert(4, "C#")

listbox.pack()

windows.mainloop()

In next article – We will create GUI notepad using menu widget in which we can create new file, save file, open file and edit.

Thank you for reading this article. I hope this article would help you.

Have a nice day

You may also like...

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: