Unit 1
Unit 1
Introduction
Tkinter tutorial provides basic and advanced concepts of Python Tkinter. Our Tkinter tutorial
is designed for beginners and professionals.
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. An empty
Tkinter top-level window can be created by using the following steps.
Tkinter widgets
There are various widgets like button, canvas, checkbutton, entry, etc. that are used to build
the python GUI applications.
SN Widget Description
The Checkbutton is
used to display the
3 Checkbutton
CheckButton on the
window.
commonly used to
accept user values.
It can be defined as a
container to which,
5 Frame
another widget can be
added and organized.
The Radiobutton is
different from a
checkbutton. Here, the
11 Radiobutton user is provided with
various options and the
user can select only one
option among them.
UNIT-1 TKINTER WIDGET
It is used to create a
14 Toplevel separate window
container.
It is an entry widget
15 Spinbox used to select from
options of values.
It is like a container
widget that contains
16 PanedWindow
horizontal or vertical
panes.
A LabelFrame is a
17 LabelFrame container widget that
acts as the container
However, the controls are less and widgets are generally added in the less organized manner.
syntax
widget.pack(options)
o expand: If the expand is set to true, the widget expands to fill any space.
o 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.
o size: it represents the side of the parent to which the widget is to be placed on the window.
This is a more organized way to place the widgets to the python application. The syntax to
use the grid() is given below.
Syntax
1. widget.grid(options)
A list of possible options that can be passed inside the grid() method is given below.
o Column
The column number in which the widget is to be placed. The leftmost column is
represented by 0.
o Columnspan
The width of the widget. It represents the number of columns up to which, the column is
expanded.
UNIT-1 TKINTER WIDGET
o ipadx, ipady
It represents the number of pixels to pad the widget inside the widget's border.
o padx, pady
It represents the number of pixels to pad the widget outside the widget's border.
o row
The row number in which the widget is to be placed. The topmost row is represented by
0.
o rowspan
The height of the widget, i.e. the number of the row up to which the widget is expanded.
o Sticky
If the cell is larger than a widget, then sticky is used to specify the position of the widget
inside the cell. It may be the concatenation of the sticky letters representing the position
of the widget. It may be N, E, W, S, NE, NW, NS, EW, ES.
Syntax
widget.place(options)
o Anchor: It represents the exact position of the widget within the container. The default
value (direction) is NW (the upper left corner)
o 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.
o height, width: It refers to the height and width in pixels.
o relheight, relwidth: It is represented as the float between 0.0 and 1.0 indicating the
fraction of the parent's height and width.
o relx, rely: It is represented as the float between 0.0 and 1.0 that is the offset in the
horizontal and vertical direction.
o x, y: It refers to the horizontal and vertical offset in the pixels.
We can also associate a method or function with a button which is called when the button is
pressed.
Syntax
W = Button(parent, options)
UNIT-1 TKINTER WIDGET
Options
It represents the
background of the
1 activebackground button when the
mouse hover the
button.
It represents the
3 Bd
border width in pixels.
It represents the
4 Bg background color of
the button.
Foreground color of
6 Fg
the button.
Additional padding to
13 Padx the button in the
horizontal direction.
Additional padding to
14 pady the button in the
vertical direction.
Syntax
w = canvas(parent, options)
Options
The represents the
1 bd border width. The
default width is 2.
It represents the
2 bg background color of
the canvas.
It represents the
6 highlightcolor highlight color when
the widget is focused.
It represents the
coordinates specified
8 scrollregion as the tuple containing
the area of the
canvas.
If it is set to a positive
value. The canvas is
10 xscrollincrement
placed only to the
multiple of this value.
Works like
xscrollincrement, but
12 yscrollincrement
governs vertical
movement.
If the canvas is
scrollable, this
13 yscrollcommand attribute should be the
.set() method of the
vertical scrollbar.
The Checkbutton can contain the text or images. The Checkbutton is mostly used to provide
many choices to the user among which, the user needs to choose the one. It generally
implements many of many selections.
Syntax
w = checkbutton(master, options)
Options
It represents the
background color when
1 activebackground
the checkbutton is under
the cursor.
It represents the
2 activeforeground
foreground color of the
checkbutton when the
UNIT-1 TKINTER WIDGET
It displays an image
4 bitmap (monochrome) on the
button.
It is associated with a
function to be called
6 command
when the state of the
checkbutton is changed.
It represents the
associated variable that
25 variable
tracks the state of the
checkbutton.
Methods
It is called to turn off
1 deselect()
the checkbutton.
The checkbutton is
flashed between the
2 flash()
active and normal
colors.
It is called to turn on
4 select()
the checkbutton.
It is used to toggle
5 toggle() between the different
Checkbuttons.
Syntax
w = Entry (parent, options)
Options
The background color
1 bg
of the widget.
rectangle that is
drawn around the
widget when it has
the input focus.
It represents a non-
negative value
indicating the width of
the highlight rectangle
9 highlightthickness
to draw around the
outside of the widget
when it has the input
focus.
It represents a non-
negative value
indicating the width of
the 3-D border to
draw around the
11 insertborderwidth
insertion cursor. The
value may have any
of the forms
acceptable to
Tk_GetPixels.
It represents a non-
negative integer value
indicating the number
12 insertofftime
of milliseconds the
insertion cursor
should remain "off" in
each blink cycle. If
UNIT-1 TKINTER WIDGET
Specifies a non-
negative integer value
indicating the number
13 insertontime of milliseconds the
insertion cursor
should remain "on" in
each blink cycle.
It represents the
value indicating the
total width of the
insertion cursor. The
14 insertwidth
value may have any
of the forms
acceptable to
Tk_GetPixels.
It is set to the
instance of the
21 textvariable StringVar to retrieve
the text from the
entry.
Methods
It is used to delete the
1 delete(first, last = none) specified characters
inside the widget.
Syntax
w = Frame(parent, options)
Options
It represents the border
1 bd
width.
There are the various options which can be specified to configure the text or the part of the
text shown in the Label.
Syntax
w = Label (master, options)
Options
It specifies the exact
position of the text
within the size
provided to the widget.
1 anchor The default value is
CENTER, which is
used to center the text
within the specified
space.
It is used to represent
the orientation of the
text if the text contains
multiple lines. It can
10 justify be set to LEFT for left
justification, RIGHT for
right justification, and
CENTER for center
justification.
The horizontal
11 padx padding of the text.
The default value is 1.
The user can choose one or more items from the list depending upon the configuration.
w = Listbox(parent, options)
Options
UNIT-1 TKINTER WIDGET
It is used to determine
the number of items that
can be selected from the
11 selectmode list. It can set to
BROWSE, SINGLE,
MULTIPLE,
EXTENDED.
Methods
It is used to select the
1 activate(index) lines at the specified
index.
It returns a tuple
containing the line
numbers of the selected
2 curselection() element or elements,
counting from 0. If
nothing is selected,
returns an empty tuple.
The Menubutton is used to implement various types of menus in the python application. A
Menu is associated with the Menubutton that can display the choices of the Menubutton when
clicked by the user.
Syntax
w = Menubutton(Top, options)
Options
UNIT-1 TKINTER WIDGET
It specifies the
4 bg background color of
the widget.
It is set to the
graphical content
5 bitmap which is to be
displayed to the
widget.
It direction can be
specified so that menu
can be displayed to
the specified direction
8 direction
of the button. Use
LEFT, RIGHT, or
ABOVE to place the
widget accordingly.
The normal
10 fg foreground color of the
widget.
The horizontal
16 padx
padding of the widget.
The top-level menus are the one which is displayed just under the title bar of the parent
window. We need to create a new instance of the Menu widget and add various commands
to it by using the add() method.
Syntax
w = Menu(top, options)
Options
The background color of
the widget when the
1 activebackground
widget is under the
focus.
It is used to display an
12 image
image on the menu.
Methods
checkbuttons to the
menu.
It is used to create a
hierarchical menu to
the parent menu by
4 add_cascade(options)
associating the
given menu to the
parent menu.
It is used to delete
the menu items
7 delete(startindex, endindex)
exist in the specified
range.
It is used to
configure a menu
8 entryconfig(index, options)
item identified by
the given index.
It is used to insert a
10 insert_seperator(index) seperator at the
specified index.
UNIT-1 TKINTER WIDGET
It is used to invoke
the associated with
11 invoke(index)
the choice given at
the specified index.
The message text contains more than one line. However, the message can only be shown in
the single font.
Syntax
w = Message(parent, options)
Options
It is used to decide the
exact position of the
text within the space
provided to the widget
1 anchor
if the widget contains
more space than the
need of the text. The
default is CENTER.
It is used to display
3 bitmap
the graphics on the
widget. It can be set to
UNIT-1 TKINTER WIDGET
any graphical or
image object.
The horizontal
11 padx
padding of the widget.
It specifies the
horizontal dimension
17 width of the widget in the
number of characters
(not pixel).
UNIT-1 TKINTER WIDGET
We can display the multiple line text or images on the radiobuttons. To keep track the user's
selection the radiobutton, it is associated with a single variable. Each button displays a single
value for that particular variable.
Syntax
w = Radiobutton(top, options)
Options
The background color of
1 activebackground the widget when it has
the focus.
It represents the
justification of the multi-
15 justify line text. It can be set to
CENTER(default), LEFT, or
RIGHT.
Methods
It is used to turn of the
1 deselect()
radiobutton.
We can control the minimum and maximum values along with the resolution of the scale. It
provides an alternative to the Entry widget when the user is forced to select only one value
from the given range of values.
Syntax
w = Scale(top, options)
Options
The background color
1 activebackground of the widget when it
has the focus.
It is set to the
procedure which is
called each time
when we move the
4 command
slider. If the slider is
moved rapidly, the
callback is done when
it settles.
It is used to represent
9 from_ one end of the widget
range.
It represents the
length of the widget. It
represents the X
13 length dimension if the scale
is horizontal or y
dimension if the scale
is vertical.
It is set to the
smallest change
17 resolution
which is to be made
to the scale value.
It represents the
length of the slider
window along the
length of the scale.
19 sliderlength
The default is 30
pixels. However, we
can change it to the
appropriate value.
DISABLED to make it
unresponsive.
It represents a float or
integer value that
specifies the other
23 to
end of the range
represented by the
scale.
It represents the
25 variable control variable for
the scale.
Methods
UNIT-1 TKINTER WIDGET
Syntax
w = Scrollbar(top, options)
Options
The background color
1 activebackground of the widget when it
has the focus.
which can be an
arrow, dot, etc.
It represents the
border width around
6 elementborderwidth the arrow heads and
slider. The default
value is -1.
The focus
highlighcolor when
7 Highlightbackground
the widget doesn't
have the focus.
The focus
highlighcolor when
8 highlighcolor
the widget has the
focus.
It represents the
9 highlightthickness thickness of the focus
highlight.
It is used to control
the behavior of the
scroll jump. If it set to
10 jump 1, then the callback is
called when the user
releases the mouse
button.
It can be set to
HORIZONTAL or
VERTICAL
11 orient
depending upon the
orientation of the
scrollbar.
UNIT-1 TKINTER WIDGET
It represents the
16 width
width of the scrollbar.
Methods
It returns the two
numbers a and b
1 get() which represents the
current position of the
scrollbar.
It is used to connect
the scrollbar to the
2 set(first, last)
other widget w. The
yscrollcommand or
xscrollcommand of the
UNIT-1 TKINTER WIDGET
The Text widget is used to display the multi-line formatted text with various styles and
attributes. The Text widget is mostly used to provide the text editor to the user.
The Text widget also facilitates us to use the marks and tabs to locate the specific sections
of the Text. We can also use the windows and images with the Text as it can also be used to
display the formatted text.
Syntax
w = Text(top, options)
Options
The background color
1 bg
of the widget.
Methods
This method is used to
delete(startindex,
1 delete the characters
endindex)
of the specified range.
It returns the
get(startindex,
2 characters present in
endindex)
the specified range.
It returns a boolean
value true or false
depending upon
5 see(index)
whether the text at the
specified index is
visible or not.
Tkinter Toplevel
The Toplevel widget is used to create and display the toplevel windows which are directly
managed by the window manager. The toplevel widget may or may not have the parent
window on the top of them.
The toplevel widget is used when a python application needs to represent some extra
information, pop-up, or the group of widgets on the new window.
The toplevel windows have the title bars, borders, and other window decorations.
UNIT-1 TKINTER WIDGET
Syntax:
w = Toplevel(options)
Options
It represents the
1 bg background color of
the window.
It represents the
2 bd border size of the
window.
It represents the
7 height
height of the window.
UNIT-1 TKINTER WIDGET
Methods
This method is used to
1 deiconify()
display the window.
It is used to show a
2 frame() system dependent
window identifier.
It is used to convert
4 iconify() the toplevel window
into an icon.
It is used to mention a
protocol(name, function which will be
5
function) called for the specific
protocol.
transient window
(temporary).
It is used to declare
9 maxsize(width, height) the maximum size for
the window.
It is used to declare
10 minsize(width, height) the minimum size for
the window.
It is used to control
resizable(width, whether the window
12
height) can be resizable or
not.
It is used in the case where a user is given some fixed number of values to choose from.
UNIT-1 TKINTER WIDGET
We can use various options with the Spinbox to decorate the widget. The syntax to use the
Spinbox is given below.
Syntax
w = Spinbox(top, options)
Options
The background color of
1 activebackground the widget when it has
the focus.
It is similar to
15 repeatinterval repeatdelay. The value
is given in milliseconds.
It is associated to the
function callback which
20 validatecommand
is used for the validation
of the widget content.
It is same as validation
22 vcmd
command.
Methods
This method is used to
delete(startindex, delete the characters
1
endindex) present at the
specified range.
It is used to identify
the widget's element
3 identify(x, y)
within the specified
range.
Tkinter PanedWindow
The PanedWindow widget acts like a Container widget which contains one or more child
widgets (panes) arranged horizontally or vertically. The child panes can be resized by the
user, by moving the separator lines known as sashes by using the mouse.
Each pane contains only one widget. The PanedWindow is used to implement the different
layouts in the python applications.
Syntax
w= PanedWindow(master, options)
Options
It represents
the background
color of the
1 bg
widget when it
doesn't have
the focus.
It represents
the 3D border
size of the
widget. The
default option
specifies that
the trough
2 bd
contains no
border whereas
the arrowheads
and slider
contain the 2-
pixel border
size.
It represents
the border
width of the
3 borderwidth
widget. The
default is 2
pixel.
The mouse
pointer is
changed to the
4 cursor specified cursor
type when it is
over the
window.
UNIT-1 TKINTER WIDGET
This option
represents the
distance
between the
handle and the
end of the
sash. For the
5 handlepad horizontal
orientation, it is
the distance
between the
top of the sash
and the handle.
The default is 8
pixels.
It represents
the size of the
handle. The
default size is 8
6 handlesize pixels.
However, the
handle will
always be a
square.
It represents
the height of
the widget. If
we do not
specify the
7 height
height, it will be
calculated by
the height of
the child
window.
It represents
the type of the
9 relief
border. The
default is FLAT.
It represents
the padding to
10 sashpad be done around
each sash. The
default is 0.
It represents
the type of the
border around
11 sashrelief
each of the
sash. The
default is FLAT.
It represents
the width of the
12 sashwidth sash. The
default is 2
pixels.
It is set to True
to display the
13 showhandle handles. The
default value is
false.
It represents
the width of the
widget. If we
14 Width
don't specify
the width of the
widget, it will be
calculated by
UNIT-1 TKINTER WIDGET
Methods
It is used to add a
1 add(child, options) window to the parent
window.
It is used to configure
3 config(options) the widget with the
specified options.
Tkinter LabelFrame
The LabelFrame widget is used to draw a border around its child widgets. We can also display
the title for the LabelFrame widget. It acts like a container which can be used to group the
number of interrelated widgets such as Radiobuttons.
This widget is a variant of the Frame widget which has all the features of a frame. It also can
display a label.
Syntax
w = LabelFrame(top, options)
Options
The default is 2
pixels.
It can be set to a
cursor type, i.e.
arrow, dot, etc. the
6 cursor mouse pointer is
changed to the cursor
type when it is over
the widget.
It represents the
7 fg foreground color of
the widget.
UNIT-1 TKINTER WIDGET
It represents the
9 height
height of the widget.
It represents the
exact position of the
10 labelAnchor text within the widget.
The default is
NW(north-west)
It represents the
widget to be used for
the label. The frame
11 labelwidget
uses the text for the
label if no value
specified.
The horizontal
15 padx
padding of the widget.
It represents the
border style. The
17 relief
default value is
GROOVE.
It represents the
18 text string containing the
label text.
19 width
Tkinter messagebox
The messagebox module is used to display the message boxes in the python applications.
There are the various functions which are used to display the relevant messages depending
upon the application requirements.
Syntax
1. messagebox.function_name(title, message [, options])
Parameters
o function_name: It represents an appropriate message box function.
o title: It is a string which is shown as a title of a message box.
o message: It is the string to be displayed as a message on the message box.
o options: There are various options which can be used to configure the message dialog
box.
The two options that can be used are default and parent.
1. default
The default option is used to mention the types of the default button, i.e. ABORT, RETRY, or
IGNORE in the message box.
2. parent
The parent option specifies the parent window on top of which, the message box is to be
displayed.
UNIT-1 TKINTER WIDGET
There is one of the following functions used to show the appropriate message boxes. All the
functions are used with the same syntax but have the specific functionalities.
1. showinfo()
The showinfo() messagebox is used where we need to show some relevant information to
the user.
2. showwarning()
This method is used to display the warning to the user.
3. showerror()
This method is used to display the error message to the user.
4. askquestion()
This method is used to ask some question to the user which can be answered in yes or no.
5. askokcancel()
This method is used to confirm the user's action regarding some application activity.
6. askyesno()
This method is used to ask the user about some action to which, the user can answer in yes
or no.
7. askretrycancel()
This method is used to ask the user about doing a particular task again or not.