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:
Widgets | Description |
---|---|
Label | It is used to display text or image on the screen |
Button | It is used to add buttons to your application |
Canvas | It is used to draw pictures and others layouts like texts, graphics etc. |
ComboBox | It contains a down arrow to select from list of available options |
CheckButton | It displays a number of options to the user as toggle buttons from which user can select any number of options. |
Entry | It is used to input single line text entry from user |
Frame | It is used as container to hold and organize the widgets |
Message | It works same as that of label and refers to multi-line and non-editable text |
Scale | It is used to provide a graphical slider which allows to select any value from that scale |
SpinBox | It is allows user to select from given set of values |
Text | It allows user to edit multiline text and format the way it has to be displayed |
Menu | It is used to create all kinds of menu used by an application |
List | Used to display a list of options for the user to select. |
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

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 –

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 –

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 –

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 –

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 –

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 –

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 –

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 –

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 –

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 –
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 –

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 –

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 –

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 must log in to post a comment.