Tkinter
Tkinter
Tkinter
• Python provides the standard library Tkinter for creating the graphical
user interface for desktop based applications.
• Developing desktop-based applications with Python Tkinter is not a
complex task. The following steps can be used to create an empty
Tkinter top-level window.
1.import the Tkinter module.
2.Create the main application window.
3.Add the widgets like labels, buttons, frames, etc. to the window.
4.Call the main event loop so the actions can occur on the user's computer
screen.
Example
SN Widget Description
1 Button The Button is used to add various kinds of buttons to the python application.
2 Canvas The canvas widget is used to draw the canvas on the window.
3 Checkbutton The Checkbutton is used to display the CheckButton on the window.
The entry widget is used to display the single-line text field to the user. It is
4 Entry
commonly used to accept user values.
It can be defined as a container to which, another widget can be added and
5 Frame
organized.
A label is a text used to display some message or information about the
6 Label
other widgets.
7 ListBox The ListBox widget is used to display a list of options to the user.
8 Menubutton The Menubutton is used to display the menu items to the user.
9 Menu It is used to add menu items to the user.
10 Message The Message widget is used to display the message-box to the user.
SN Widget Description
The Radiobutton is different from a checkbutton. Here, the user is
11 Radiobutton provided with various options and the user can select only one
option among them.
12 Scale It is used to provide the slider to the user.
It provides the scrollbar to the user so that the user can scroll the
13 Scrollbar window up and down.
It is different from Entry because it provides a multi-line text field to
14 Text the user so that the user can write the text and edit the text inside
it.
14 Toplevel It is used to create a separate window container.
15 Spinbox It is an entry widget used to select from options of values.
It is like a container widget that contains horizontal or vertical
16 PanedWind panes.
ow
17 LabelFrame A LabelFrame is a container widget that acts as the container
This module is used to display the message-box in the desktop
18 MessageBox
based applications.
Python Tkinter Geometry
• widget.pack(options)
• A list of possible options that can be passed in pack() is given below.
• expand: If the expand is set to true, the widget expands to fill any space.
• Fill: By default, the fill is set to NONE. However, we can set it to X or Y to
determine whether the widget contains any extra space.
• size: it represents the side of the parent to which the widget is to be placed on
the window.
Example
• Syntax
• widget.place(options)
• A list of possible options is given below.
• Anchor: It represents the exact position of the widget within the
container. The default value (direction) is NW (the upper left corner)
• bordermode: The default value of the border type is INSIDE that
refers to ignore the parent's inside the border. The other option is
OUTSIDE.
• height, width: It refers to the height and width in pixels.
• relheight, relwidth: It is represented as the float between 0.0 and 1.0
indicating the fraction of the parent's height and width.
• relx, rely: It is represented as the float between 0.0 and 1.0 that is the
offset in the horizontal and vertical direction.
• x, y: It refers to the horizontal and vertical offset in the pixels.
Example
10 Highlightcolor The color of the highlight when the button has the focus.
11 Image It is set to the image displayed on the button.
It illustrates the way by which the multiple text lines
are represented. It is set to LEFT for left justification,
12 justify
RIGHT for the right justification, and CENTER for the
center.
Additional padding to the button in the horizontal
13 Padx
direction.
Additional padding to the button in the vertical
14 pady
direction.
It represents the type of the border. It can be SUNKEN,
15 Relief
RAISED, GROOVE, and RIDGE.
This option is set to DISABLED to make the button
17 State unresponsive. The ACTIVE represents the active state
of the button.
18 Underline Set this option to make the button text underlined.
The width of the button. It exists as a number of
19 Width
letters for textual buttons or pixels for image buttons.
If the value is set to a positive number, the text lines
20 Wraplength
will be wrapped to fit within this length.
Example
#python application to create a simple button
top = Tk()
top.geometry("200x100")
b = Button(top,text = "Simple")
b.pack()
top.mainaloop()
from tkinter import *
top = Tk()
top.geometry("200x100")
def fun():
messagebox.showinfo("Hello", "Red Button clicked")
b1.pack(side = LEFT)
b2.pack(side = RIGHT)
b3.pack(side = TOP)
b4.pack(side = BOTTOM)
top.mainloop()
Python Tkinter Canvas
7 relief It represents the type of the border. The possible values are
SUNKEN, RAISED, GROOVE, and RIDGE.
top.mainloop()
from tkinter import *
top = Tk()
top.geometry("200x200")
c.pack()
top.mainloop()
Python Tkinter Checkbutton
12 highlightcolor The color of the focus highlight when the checkbutton is under focus.
14 justify This specifies the justification of the text if the text contains multiple lines.
The associated control variable is set to 0 by default if the button is unchecked. We can
15 offvalue
change the state of an unchecked variable to some other one.
The associated control variable is set to 1 by default if the button is checked. We can
16 onvalue
change the state of the checked variable to some other one.
19 relief The type of the border of the checkbutton. By default, it is set to FLAT.
SN Method Description
1 deselect() It is called to turn off the checkbutton.
The checkbutton is flashed between the active and
2 flash() normal colors.
This will invoke the method associated with the
3 invoke() checkbutton.
4 select() It is called to turn on the checkbutton.
It is used to toggle between the different
5 toggle() Checkbuttons.
from tkinter import *
top = Tk()
top.geometry("200x200")
checkvar1 = IntVar()
checkvar2 = IntVar()
checkvar3 = IntVar()
chkbtn1 = Checkbutton(top, text = "C", variable = checkvar1, onvalue = 1, offvalue = 0, height = 2, width = 10)
chkbtn2 = Checkbutton(top, text = "C++", variable = checkvar2, onvalue = 1, offvalue = 0, height = 2, width = 10)
chkbtn3 = Checkbutton(top, text = "Java", variable = checkvar3, onvalue = 1, offvalue = 0, height = 2, width = 10)
chkbtn1.pack()
chkbtn2.pack()
chkbtn3.pack()
top.mainloop()
Python Tkinter Entry
• The Entry widget is used to provde the single line text-box to the user
to accept a value from the user. We can use the Entry widget to
accept the text strings from the user. It can only be used for one line
of text from the user. For multiple lines of text, we must use the text
widget.
The syntax to use the Entry widget is given below.
• Syntax
w = Entry (parent, options)
Entry*
Example
# !/usr/bin/python3
top = Tk()
top.geometry("400x250")
top.mainloop()
import tkinter as tk
from functools import partial
def call_result(label_result, n1, n2):
num1 = (n1.get())
num2 = (n2.get())
result = int(num1)+int(num2)
label_result.config(text="Result = %d" % result)
return
root = tk.Tk()
root.geometry('400x200+100+200')
root.title('Calculator')
number1 = tk.StringVar()
number2 = tk.StringVar()
root.mainloop()
Listbox