Tkinter Javapoint PDF
Tkinter Javapoint PDF
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.
Example
Output:
Tkinter widgets
There are various widgets like button, canvas, checkbutton, entry, etc. that are used to build the
python GUI applications.
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.
The entry widget is used to display the single-line text field to the user. It is commonly
4 Entry
used to accept user values.
5 Frame It can be defined as a container to which, another widget can be added and organized.
6 Label A label is a text used to display some message or information about the 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.
10 Message The Message widget is used to display the message-box to the user.
The Radiobutton is different from a checkbutton. Here, the user is provided with various
11 Radiobutton
options and the user can select only one option among them.
13 Scrollbar It provides the scrollbar to the user so that the user can scroll the window up and down.
It is different from Entry because it provides a multi-line text field to the user so that the
14 Text
user can write the text and edit the text inside it.
18 MessageBox This module is used to display the message-box in the desktop based applications.
Python Tkinter Geometry
The Tkinter geometry specifies the method by using which, the widgets are represented on display.
The python Tkinter provides the following geometry methods.
The pack() widget is used to organize widget in the block. The positions widgets added to the
python application using the pack() method can be controlled by using the various options specified
in the method call.
However, the controls are less and widgets are generally added in the less organized manner.
Syntax
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
Output:
The grid() geometry manager organizes the widgets in the tabular form. We can specify the rows
and columns as the options in the method call. We can also specify the column span (width) or
rowspan(height) of a widget.
This is a more organized way to place the widgets to the python application. The syntax to use the
grid() is given below.
Syntax
A list of possible options that can be passed inside the grid() method is given below.
Column
The column number in which the widget is to be placed. The leftmost column is represented
by 0.
Columnspan
The width of the widget. It represents the number of columns up to which, the column is
expanded.
ipadx, ipady
It represents the number of pixels to pad the widget inside the widget's border.
padx, pady
It represents the number of pixels to pad the widget outside the widget's border.
row
The row number in which the widget is to be placed. The topmost row is represented by 0.
rowspan
The height of the widget, i.e. the number of the row up to which the widget is expanded.
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.
Example
Output:
The place() geometry manager organizes the widgets to the specific x and y coordinates.
Syntax
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
Output:
Prerequisite
Before learning Tkinter, you must have the basic knowledge of Python.
Audience
Our Python Tkinter tutorial is designed to help beginners and professionals.
Problem
We assure that you will not find any problem in this Tkinter tutorial. But if there is any mistake,
please post the problem in contact form.
Python Tkinter Button
The button widget is used to add various types of buttons to the python application. Python allows
us to configure the look of the button according to our requirements. Various options can be set or
reset depending upon the requirements.
We can also associate a method or function with a button which is called when the button is
pressed.
Syntax
SN Option Description
1 activebackground It represents the background of the button when the mouse hover the button.
2 activeforeground It represents the font color of the button when the mouse hover the button.
3 Bd It represents the border width in pixels.
4 Bg It represents the background color of the button.
5 Command It is set to the function call which is scheduled when the function is called.
6 Fg Foreground color of the button.
7 Font The font of the button text.
The height of the button. The height is represented in the number of text lines
8 Height
for the textual lines or the number of pixels for the images.
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
12 justify to LEFT for left justification, RIGHT for the right justification, and CENTER
for the center.
13 Padx Additional padding to the button in the horizontal direction.
14 pady Additional padding to the button in the vertical direction.
It represents the type of the border. It can be SUNKEN, RAISED, GROOVE,
15 Relief
and RIDGE.
This option is set to DISABLED to make the button unresponsive. The
17 State
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 letters for textual buttons or
19 Width
pixels for image buttons.
If the value is set to a positive number, the text lines will be wrapped to fit
20 Wraplength
within this length.
Example
Output:
Example
Output:
Python Tkinter Canvas
The canvas widget is used to add the structured graphics to the python application. It is used to
draw the graph and plots to the python application. The syntax to use the canvas is given below.
Syntax
SN Option Description
1 bd The represents the border width. The default width is 2.
2 bg It represents the background color of the canvas.
3 confine It is set to make the canvas unscrollable outside the scroll region.
4 cursor The cursor is used as the arrow, circle, dot, etc. on the canvas.
5 height It represents the size of the canvas in the vertical direction.
6 highlightcolor It represents the highlight color when the widget is focused.
It represents the type of the border. The possible values are SUNKEN,
7 relief
RAISED, GROOVE, and RIDGE.
It represents the coordinates specified as the tuple containing the area of the
8 scrollregion
canvas.
9 width It represents the width of the canvas.
If it is set to a positive value. The canvas is placed only to the multiple of this
10 xscrollincrement
value.
If the canvas is scrollable, this attribute should be the .set() method of the
11 xscrollcommand
horizontal scrollbar.
12 yscrollincrement Works like xscrollincrement, but governs vertical movement.
If the canvas is scrollable, this attribute should be the .set() method of the
13 yscrollcommand
vertical scrollbar.
Example
Output:
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
SN Option Description
1 activebackground It represents the background color when the checkbutton is under the cursor.
It represents the foreground color of the checkbutton when the checkbutton is
2 activeforeground
under the cursor.
3 bg The background color of the button.
4 bitmap It displays an image (monochrome) on the button.
5 bd The size of the border around the corner.
It is associated with a function to be called when the state of the checkbutton
6 command
is changed.
The mouse pointer will be changed to the cursor name when it is over the
7 cursor
checkbutton.
8 disableforeground It is the color which is used to represent the text of a disabled checkbutton.
9 font It represents the font of the checkbutton.
10 fg The foreground color (text color) of the checkbutton.
It represents the height of the checkbutton (number of lines). The default
11 height
height is 1.
12 highlightcolor The color of the focus highlight when the checkbutton is under focus.
13 image The image used to represent the checkbutton.
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
15 offvalue unchecked. We can 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.
16 onvalue
We can change the state of the checked variable to some other one.
17 padx The horizontal padding of the checkbutton
18 pady The vertical padding of the checkbutton.
19 relief The type of the border of the checkbutton. By default, it is set to FLAT.
20 selectcolor The color of the checkbutton when it is set. By default, it is red.
21 selectimage The image is shown on the checkbutton when it is set.
It represents the state of the checkbutton. By default, it is set to normal. We
22 state can change it to DISABLED to make the checkbutton unresponsive. The
state of the checkbutton is ACTIVE when it is under focus.
It represents the index of the character in the text which is to be underlined.
24 underline
The indexing starts with zero in the text.
25 variable It represents the associated variable that tracks the state of the checkbutton.
It represents the width of the checkbutton. It is represented in the number of
26 width
characters that are represented in the form of texts.
If this option is set to an integer number, the text will be broken into the
27 wraplength
number of pieces.
Methods
The methods that can be called with the Checkbuttons are described in the following table.
SN Method Description
1 deselect() It is called to turn off the checkbutton.
2 flash() The checkbutton is flashed between the active and normal colors.
3 invoke() This will invoke the method associated with the checkbutton.
4 select() It is called to turn on the checkbutton.
5 toggle() It is used to toggle between the different Checkbuttons.
Example
Output:
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.
Syntax
SN Option Description
1 bg The background color of the widget.
2 bd The border width of the widget in pixels.
The mouse pointer will be changed to the cursor type set to the arrow, dot,
3 cursor
etc.
The text written inside the entry box will be automatically copied to the
4 exportselection
clipboard by default. We can set the exportselection to 0 to not copy this.
5 fg It represents the color of the text.
6 font It represents the font type of the text.
It represents the color to display in the traversal highlight region when the
7 highlightbackground
widget does not have the input focus.
It represents the color to use for the traversal highlight rectangle that is
8 highlightcolor
drawn around the widget when it has the input focus.
It represents a non-negative value indicating the width of the highlight
9 highlightthickness rectangle to draw around the outside of the widget when it has the input
focus.
It represents the color to use as background in the area covered by the
10 insertbackground insertion cursor. This color will normally override either the normal
background for the widget.
It represents a non-negative value indicating the width of the 3-D border to
11 insertborderwidth draw around the insertion cursor. The value may have any of the forms
acceptable to Tk_GetPixels.
It represents a non-negative integer value indicating the number of
12 insertofftime milliseconds the insertion cursor should remain "off" in each blink cycle.
If this option is zero, then the cursor doesn't blink: it is on all the time.
Specifies a non-negative integer value indicating the number of
13 insertontime
milliseconds the insertion cursor should remain "on" in each blink cycle.
It represents the value indicating the total width of the insertion cursor.
14 insertwidth
The value may have any of the forms acceptable to Tk_GetPixels.
15 justify It specifies how the text is organized if the text contains multiple lines.
16 relief It specifies the type of the border. Its default value is FLAT.
17 selectbackground The background color of the selected text.
18 selectborderwidth The width of the border to display around the selected task.
19 selectforeground The font color of the selected task.
It is used to show the entry text of some other type instead of the string.
20 show
For example, the password is typed using stars (*).
21 textvariable It is set to the instance of the StringVar to retrieve the text from the entry.
22 width The width of the displayed text or image.
The entry widget can be linked to the horizontal scrollbar if we want the
23 xscrollcommand
user to enter more text then the actual width of the widget.
Example
Output:
SN Method Description
1 delete(first, last = none) It is used to delete the specified characters inside the widget.
2 get() It is used to get the text written inside the widget.
It is used to change the insertion cursor position. We can specify the
3 icursor(index)
index of the character before which, the cursor to be placed.
It is used to place the cursor to the left of the character written at the
4 index(index)
specified index.
It is used to insert the specified string before the character placed at
5 insert(index,s)
the specified index.
It includes the selection of the character present at the specified
6 select_adjust(index)
index.
7 select_clear() It clears the selection if some selection has been done.
It sets the anchor index position to the character specified by the
8 select_form(index)
index.
It returns true if some text in the Entry is selected otherwise returns
9 select_present()
false.
10 select_range(start,end) It selects the characters to exist between the specified range.
It selects all the characters from the beginning to the specified
11 select_to(index)
index.
12 xview(index) It is used to link the entry widget to a horizontal scrollbar.
13 xview_scroll(number,what) It is used to make the entry scrollable horizontally.
Example: A simple calculator
Output:
Python Tkinter Frame
Python Tkinter Frame widget is used to organize the group of widgets. It acts like a container which
can be used to hold the other widgets. The rectangular areas of the screen are used to organize the
widgets to the python application.
Syntax
SN Option Description
1 bd It represents the border width.
2 bg The background color of the widget.
The mouse pointer is changed to the cursor type set to different values like
3 cursor
an arrow, dot, etc.
4 height The height of the frame.
5 highlightbackground The color of the background color when it is under focus.
6 highlightcolor The text color when the widget is under focus.
It specifies the thickness around the border when the widget is under the
7 highlightthickness
focus.
8 relief It specifies the type of the border. The default value if FLAT.
9 width It represents the width of the widget.
Example
Output:
Python Tkinter Label
The Label is used to specify the container box where we can place the text or images. This widget is
used to provide the message to the user about other widgets used in the python application.
There are the various options which can be specified to configure the text or the part of the text
shown in the Label.
Syntax
SN Option Description
It specifies the exact position of the text within the size provided to the widget. The
1 anchor default value is CENTER, which is used to center the text within the specified
space.
2 bg The background color displayed behind the widget.
It is used to set the bitmap to the graphical object specified so that, the label can
3 bitmap
represent the graphics instead of text.
4 bd It represents the width of the border. The default is 2 pixels.
The mouse pointer will be changed to the type of the cursor specified, i.e., arrow,
5 cursor
dot, etc.
6 font The font type of the text written inside the widget.
7 fg The foreground color of the text written inside the widget.
8 height The height of the widget.
9 image The image that is to be shown as the label.
It is used to represent the orientation of the text if the text contains multiple lines. It
10 justify can be set to LEFT for left justification, RIGHT for right justification, and
CENTER for center justification.
11 padx The horizontal padding of the text. The default value is 1.
12 pady The vertical padding of the text. The default value is 1.
13 relief The type of the border. The default value is FLAT.
14 text This is set to the string variable which may contain one or more line of text.
The text written inside the widget is set to the control variable StringVar so that it
15 textvariable
can be accessed and changed accordingly.
We can display a line under the specified letter of the text. Set this option to the
16 underline
number of the letter under which the line will be displayed.
17 width The width of the widget. It is specified as the number of characters.
Instead of having only one line as the label text, we can break it to the number of
18 wraplength
lines where each line has the number of characters specified to this option.
Example 1
Output:
Python Tkinter Listbox
The Listbox widget is used to display the list items to the user. We can place only text items in the
Listbox and all text items contain the same font and color.
The user can choose one or more items from the list depending upon the configuration.
SN Option Description
1 bg The background color of the widget.
2 bd It represents the size of the border. Default value is 2 pixel.
3 cursor The mouse pointer will look like the cursor type like dot, arrow, etc.
4 font The font type of the Listbox items.
5 fg The color of the text.
It represents the count of the lines shown in the Listbox. The default value is
6 height
10.
7 highlightcolor The color of the Listbox items when the widget is under focus.
8 highlightthickness The thickness of the highlight.
9 relief The type of the border. The default is SUNKEN.
10 selectbackground The background color that is used to display the selected text.
It is used to determine the number of items that can be selected from the list.
11 selectmode
It can set to BROWSE, SINGLE, MULTIPLE, EXTENDED.
12 width It represents the width of the widget in characters.
13 xscrollcommand It is used to let the user scroll the Listbox horizontally.
14 yscrollcommand It is used to let the user scroll the Listbox vertically.
Methods
There are the following methods associated with the Listbox.
SN Method Description
1 activate(index) It is used to select the lines at the specified index.
It returns a tuple containing the line numbers of the selected element
2 curselection() or elements, counting from 0. If nothing is selected, returns an empty
tuple.
3 delete(first, last = None) It is used to delete the lines which exist in the given range.
4 get(first, last = None) It is used to get the list items that exist in the given range.
It is used to place the line with the specified index at the top of the
5 index(i)
widget.
6 insert(index, *elements) It is used to insert the new lines with the specified number of
elements before the specified index.
It returns the index of the nearest line to the y coordinate of the
7 nearest(y)
Listbox widget.
It is used to adjust the position of the listbox to make the lines
8 see(index)
specified by the index visible.
9 size() It returns the number of lines that are present in the Listbox widget.
10 xview() This is used to make the widget horizontally scrollable.
It is used to make the listbox horizontally scrollable by the fraction
11 xview_moveto(fraction)
of width of the longest line present in the listbox.
xview_scroll(number, It is used to make the listbox horizontally scrollable by the number of
12
what) characters specified.
13 yview() It allows the Listbox to be vertically scrollable.
It is used to make the listbox vertically scrollable by the fraction of
14 yview_moveto(fraction)
width of the longest line present in the listbox.
yview_scroll (number, It is used to make the listbox vertically scrollable by the number of
15
what) characters specified.
Example 1
Output:
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
SN Option Description
1 activebackground The background color of the widget when the widget is under focus.
2 activeforeground The font color of the widget text when the widget is under focus.
It specifies the exact position of the widget content when the widget is
3 anchor
assigned more space than needed.
4 bg It specifies the background color of the widget.
5 bitmap It is set to the graphical content which is to be displayed to the widget.
6 bd It represents the size of the border. The default value is 2 pixels.
The mouse pointer will be changed to the cursor type specified when the
7 cursor widget is under the focus. The possible value of the cursor type is arrow, or
dot etc.
It direction can be specified so that menu can be displayed to the specified
8 direction direction of the button. Use LEFT, RIGHT, or ABOVE to place the widget
accordingly.
9 disabledforeground The text color of the widget when the widget is disabled.
10 fg The normal foreground color of the widget.
The vertical dimension of the Menubutton. It is specified as the number of
11 height
lines.
12 highlightcolor The highlight color shown to the widget under focus.
13 image The image displayed on the widget.
This specified the exact position of the text under the widget when the text
is unable to fill the width of the widget. We can use the LEFT for the left
14 justify
justification, RIGHT for the right justification, CENTER for the centre
justification.
15 menu It represents the menu specified with the Menubutton.
16 padx The horizontal padding of the widget.
17 pady The vertical padding of the widget.
18 relief This option specifies the type of the border. The default value is RAISED.
19 state The normal state of the Mousebutton is enabled. We can set it to
DISABLED to make it unresponsive.
20 text The text shown with the widget.
We can set the control variable of string type to the text variable so that we
21 textvariable
can control the text of the widget at runtime.
The text of the widget is not underlined by default but we can set this option
22 underline
to make the text of the widget underlined.
23 width It represents the width of the widget in characters. The default value is 20.
We can break the text of the widget in the number of lines so that the text
24 wraplength
contains the number of lines not greater than the specified value.
Example
Output:
Python Tkinter Menu
The Menu widget is used to create various types of menus (top level, pull down, and pop up) in the
python application.
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
SN Option Description
1 activebackground The background color of the widget when the widget is under the focus.
The width of the border of the widget when it is under the mouse. The
2 activeborderwidth
default is 1 pixel.
3 activeforeground The font color of the widget when the widget has the focus.
4 bg The background color of the widget.
5 bd The border width of the widget.
The mouse pointer is changed to the cursor type when it hovers the widget.
6 cursor
The cursor type can be set to arrow or dot.
7 disabledforeground The font color of the widget when it is disabled.
8 font The font type of the text of the widget.
9 fg The foreground color of the widget.
The postcommand can be set to any of the function which is called when the
10 postcommand
mourse hovers the menu.
11 relief The type of the border of the widget. The default type is RAISED.
12 image It is used to display an image on the menu.
The color used to display the checkbutton or radiobutton when they are
13 selectcolor
selected.
By default, the choices in the menu start taking place from position 1. If we
14 tearoff
set the tearoff = 1, then it will start taking place from 0th position.
Set this option to the title of the window if you want to change the title of
15 title
the window.
Methods
The Menu widget contains the following methods.
SN Option Description
1 add_command(options) It is used to add the Menu items to the menu.
2 add_radiobutton(options) This method adds the radiobutton to the menu.
3 add_checkbutton(options) This method is used to add the 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.
5 add_seperator() It is used to add the seperator line to the menu.
6 add(type, options) It is used to add the specific menu item to the menu.
delete(startindex,
7 It is used to delete the menu items exist in the specified range.
endindex)
entryconfig(index,
8 It is used to configure a menu item identified by the given index.
options)
9 index(item) It is used to get the index of the specified menu item.
10 insert_seperator(index) It is used to insert a seperator at the specified index.
It is used to invoke the associated with the choice given at the
11 invoke(index)
specified index.
12 type(index) It is used to get the type of the choice specified by the index.
Example 1
Output:
Clicking the hello Menubutton will print the hello on the console while clicking the Quit
Menubutton will make an exit from the python application.
Example 2
Output:
Python Tkinter Message
The Message widget is used to show the message to the user regarding the behaviour of the python
application. The message widget shows the text messages to the user which can not be edited.
The message text contains more than one line. However, the message can only be shown in the
single font.
Syntax
SN Option Description
It is used to decide the exact position of the text within the space provided to the
1 anchor widget if the widget contains more space than the need of the text. The default is
CENTER.
2 bg The background color of the widget.
It is used to display the graphics on the widget. It can be set to any graphical or
3 bitmap
image object.
4 bd It represents the size of the border in the pixel. The default size is 2 pixel.
The mouse pointer is changed to the specified cursor type. The cursor type can be
5 cursor
an arrow, dot, etc.
6 font The font type of the widget text.
7 fg The font color of the widget text.
8 height The vertical dimension of the message.
9 image We can set this option to a static image to show that onto the widget.
This option is used to specify the alignment of multiple line of code with respect to
10 justify each other. The possible values can be LEFT (left alignment), CENTER (default),
and RIGHT (right alignment).
11 padx The horizontal padding of the widget.
12 pady The vertical padding of the widget.
13 relief It represents the type of the border. The default type is FLAT.
We can set this option to the string so that the widget can represent the specified
14 text
text.
This is used to control the text represented by the widget. The textvariable can be
15 textvariable
set to the text that is shown in the widget.
The default value of this option is -1 that represents no underline. We can set this
16 underline option to an existing number to specify that nth letter of the string will be
underlined.
It specifies the horizontal dimension of the widget in the number of characters (not
17 width
pixel).
We can wrap the text to the number of lines by setting this option to the desired
18 wraplength
number so that each line contains only that number of characters.
Example
Output:
Python Tkinter Radiobutton
The Radiobutton widget is used to implement one-of-many selection in the python application. It
shows multiple choices to the user out of which, the user can select only one out of them. We can
associate different methods with each of the radiobutton.
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
SN Option Description
1 activebackground The background color of the widget when it has the focus.
2 activeforeground The font color of the widget text when it has the focus.
It represents the exact position of the text within the widget if the widget
3 anchor contains more space than the requirement of the text. The default value is
CENTER.
4 bg The background color of the widget.
It is used to display the graphics on the widget. It can be set to any
5 bitmap
graphical or image object.
6 borderwidth It represents the size of the border.
This option is set to the procedure which must be called every-time when
7 command
the state of the radiobutton is changed.
The mouse pointer is changed to the specified cursor type. It can be set to
8 cursor
the arrow, dot, etc.
9 font It represents the font type of the widget text.
10 fg The normal foreground color of the widget text.
The vertical dimension of the widget. It is specified as the number of lines
11 height
(not pixel).
It represents the color of the focus highlight when the widget has the
12 highlightcolor
focus.
13 highlightbackground The color of the focus highlight when the widget is not having the focus.
It can be set to an image object if we want to display an image on the
14 image
radiobutton instead the text.
It represents the justification of the multi-line text. It can be set to
15 justify
CENTER(default), LEFT, or RIGHT.
16 padx The horizontal padding of the widget.
17 pady The vertical padding of the widget.
18 relief The type of the border. The default value is FLAT.
19 selectcolor The color of the radio button when it is selected.
20 selectimage The image to be displayed on the radiobutton when it is selected.
It represents the state of the radio button. The default state of the
21 state Radiobutton is NORMAL. However, we can set this to DISABLED to
make the radiobutton unresponsive.
22 text The text to be displayed on the radiobutton.
23 textvariable It is of String type that represents the text displayed by the widget.
The default value of this option is -1, however, we can set this option to
24 underline
the number of character which is to be underlined.
The value of each radiobutton is assigned to the control variable when it is
25 value
turned on by the user.
It is the control variable which is used to keep track of the user's choices.
26 variable
It is shared among all the radiobuttons.
The horizontal dimension of the widget. It is represented as the number of
27 width
characters.
We can wrap the text to the number of lines by setting this option to the
28 wraplength
desired number so that each line contains only that number of characters.
Methods
The radiobutton widget provides the following methods.
SN Method Description
1 deselect() It is used to turn of the radiobutton.
2 flash() It is used to flash the radiobutton between its active and normal colors few times.
3 invoke() It is used to call any procedure associated when the state of a Radiobutton is changed.
4 select() It is used to select the radiobutton.
Example
Output:
Python Tkinter Scale
The Scale widget is used to implement the graphical slider to the python application so that the user
can slide through the range of values shown on the slider and select the one among them.
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
SN Option Description
1 activebackground The background color of the widget when it has the focus.
2 bg The background color of the widget.
3 bd The border size of the widget. The default is 2 pixel.
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.
The mouse pointer is changed to the cursor type assigned to this option. It
5 cursor
can be an arrow, dot, etc.
If the control variable used to control the scale data is of string type, this
6 digits option is used to specify the number of digits when the numeric scale is
converted to a string.
7 font The font type of the widget text.
8 fg The foreground color of the text.
9 from_ It is used to represent one end of the widget range.
10 highlightbackground The highlight color when the widget doesn't have the focus.
11 highlighcolor The highlight color when the widget has the focus.
This can be set to some text which can be shown as a label with the scale.
12 label It is shown in the top left corner if the scale is horizontal or the top right
corner if the scale is vertical.
It represents the length of the widget. It represents the X dimension if the
13 length
scale is horizontal or y dimension if the scale is vertical.
14 orient It can be set to horizontal or vertical depending upon the type of the scale.
15 relief It represents the type of the border. The default is FLAT.
This option tells the duration up to which the button is to be pressed before
16 repeatdelay
the slider starts moving in that direction repeatedly. The default is 300 ms.
17 resolution It is set to the smallest change which is to be made to the scale value.
The value of the scale is shown in the text form by default. We can set this
18 showvalue
option to 0 to suppress the label.
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.
The scale widget is active by default. We can set this to DISABLED to
20 state
make it unresponsive.
The focus cycles through the scale widgets by default. We can set this
21 takefocus
option to 0 if we don't want this to happen.
The scale values are displayed on the multiple of the specified tick
22 tickinterval
interval. The default value of the tickinterval is 0.
It represents a float or integer value that specifies the other end of the
23 to
range represented by the scale.
24 troughcolor It represents the color of the through.
25 variable It represents the control variable for the scale.
26 width It represents the width of the through part of the widget.
Methods
SN Method Description
1 get() It is used to get the current value of the scale.
2 set(value) It is used to set the value of the scale.
Example
Output:
Python Tkinter Scrollbar
The scrollbar widget is used to scroll down the content of the other widgets like listbox, text, and
canvas. However, we can also create the horizontal scrollbars to the Entry widget.
Syntax
SN Option Description
1 activebackground The background color of the widget when it has the focus.
2 bg The background color of the widget.
3 bd The border width of the widget.
It can be set to the procedure associated with the list which can be called
4 command
each time when the scrollbar is moved.
The mouse pointer is changed to the cursor type set to this option which
5 cursor
can be an arrow, dot, etc.
It represents the border width around the arrow heads and slider. The
6 elementborderwidth
default value is -1.
7 Highlightbackground The focus highlighcolor when the widget doesn't have the focus.
8 highlighcolor The focus highlighcolor when the widget has the focus.
9 highlightthickness It represents the thickness of the focus highlight.
It is used to control the behavior of the scroll jump. If it set to 1, then the
10 jump
callback is called when the user releases the mouse button.
It can be set to HORIZONTAL or VERTICAL depending upon the
11 orient
orientation of the scrollbar.
This option tells the duration up to which the button is to be pressed
12 repeatdelay before the slider starts moving in that direction repeatedly. The default is
300 ms.
13 repeatinterval The default value of the repeat interval is 100.
We can tab the focus through this widget by default. We can set this
14 takefocus
option to 0 if we don't want this behavior.
15 troughcolor It represents the color of the trough.
16 width It represents the width of the scrollbar.
Methods
The widget provides the following methods.
SN Method Description
It returns the two numbers a and b which represents the current position of the
1 get()
scrollbar.
set(first, It is used to connect the scrollbar to the other widget w. The yscrollcommand or
2
last) xscrollcommand of the other widget to this method.
Example
Output:
Python Tkinter Text
The Text widget is used to show the text data on the Python application. However, Tkinter provides
us the Entry widget which is used to implement the single line text box.
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
A list of possible options that can be used with the Text widget is given below.
SN Option Description
1 bg The background color of the widget.
2 bd It represents the border width of the widget.
The mouse pointer is changed to the specified cursor type, i.e. arrow, dot,
3 cursor
etc.
The selected text is exported to the selection in the window manager. We
4 exportselection
can set this to 0 if we don't want the text to be exported.
5 font The font type of the text.
6 fg The text color of the widget.
7 height The vertical dimension of the widget in lines.
8 highlightbackground The highlightcolor when the widget doesn't has the focus.
9 highlightthickness The thickness of the focus highlight. The default value is 1.
10 highlighcolor The color of the focus highlight when the widget has the focus.
11 insertbackground It represents the color of the insertion cursor.
12 insertborderwidth It represents the width of the border around the cursor. The default is 0.
The time amount in Milliseconds during which the insertion cursor is off
13 insertofftime
in the blink cycle.
The time amount in Milliseconds during which the insertion cursor is on in
14 insertontime
the blink cycle.
15 insertwidth It represents the width of the insertion cursor.
16 padx The horizontal padding of the widget.
17 pady The vertical padding of the widget.
18 relief The type of the border. The default is SUNKEN.
19 selectbackground The background color of the selected text.
20 selectborderwidth The width of the border around the selected text.
It specifies the amount of vertical space given above each line of the text.
21 spacing1
The default is 0.
This option specifies how much extra vertical space to add between
22 spacing2
displayed lines of text when a logical line wraps. The default is 0.
It specifies the amount of vertical space to insert below each line of the
23 spacing3
text.
It the state is set to DISABLED, the widget becomes unresponsive to the
24 state
mouse and keyboard unresponsive.
25 tabs This option controls how the tab character is used to position the text.
26 width It represents the width of the widget in characters.
This option is used to wrap the wider lines into multiple lines. Set this
option to the WORD to wrap the lines after the word that fit into the
27 wrap
available space. The default value is CHAR which breaks the line which
gets too wider at any character.
To make the Text widget horizontally scrollable, we can set this option to
28 xscrollcommand
the set() method of Scrollbar widget.
To make the Text widget vertically scrollable, we can set this option to the
29 yscrollcommand
set() method of Scrollbar widget.
Methods
We can use the following methods with the Text widget.
SN Method Description
delete(startindex,
1 This method is used to delete the characters of the specified range.
endindex)
get(startindex,
2 It returns the characters present in the specified range.
endindex)
3 index(index) It is used to get the absolute index of the specified index.
4 insert(index, string) It is used to insert the specified string at the given index.
It returns a boolean value true or false depending upon whether the text
5 see(index)
at the specified index is visible or not.
SN Method Description
1 index(mark) It is used to get the index of the specified mark.
2 mark_gravity(mark, gravity) It is used to get the gravity of the given mark.
3 mark_names() It is used to get all the marks present in the Text widget.
4 mark_set(mark, index) It is used to inform a new position of the given mark.
5 mark_unset(mark) It is used to remove the given mark from the text.
Example
Output:
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.
Syntax
SN Options Description
1 bg It represents the background color of the window.
2 bd It represents the border size of the window.
The mouse pointer is changed to the cursor type set to the arrow, dot, etc. when the
3 cursor
mouse is in the window.
The text selected in the text widget is exported to be selected to the window manager.
4 class_
We can set this to 0 to make this behavior false.
5 font The font type of the text inserted into the widget.
6 fg The foreground color of the widget.
7 height It represents the height of the window.
8 relief It represents the type of the window.
9 width It represents the width of the window,
Methods
The methods associated with the Toplevel widget is given in the following list.
SN Method Description
1 deiconify() This method is used to display the window.
2 frame() It is used to show a system dependent window identifier.
3 group(window) It is used to add this window to the specified window group.
4 iconify() It is used to convert the toplevel window into an icon.
protocol(name, It is used to mention a function which will be called for the specific
5
function) protocol.
It is used to get the current state of the window. Possible values are
6 state()
normal, iconic, withdrawn, and icon.
7 transient([master]) It is used to convert this window to a transient window (temporary).
8 withdraw() It is used to delete the window but doesn't destroy it.
maxsize(width,
9 It is used to declare the maximum size for the window.
height)
10 minsize(width, height) It is used to declare the minimum size for the window.
11 positionfrom(who) It is used to define the position controller.
resizable(width,
12 It is used to control whether the window can be resizable or not.
height)
13 sizefrom(who) It is used to define the size controller.
14 title(string) It is used to define the title for the window.
Example
Output:
Python Tkinter Spinbox
The Spinbox widget is an alternative to the Entry widget. It provides the range of values to the user,
out of which, the user can select the one.
It is used in the case where a user is given some fixed number of values to choose from.
We can use various options with the Spinbox to decorate the widget. The syntax to use the Spinbox
is given below.
Syntax
SN Option Description
1 activebackground The background color of the widget when it has the focus.
2 bg The background color of the widget.
3 bd The border width of the widget.
The associated callback with the widget which is called each time the state
4 command
of the widget is called.
5 cursor The mouse pointer is changed to the cursor type assigned to this option.
6 disabledbackground The background color of the widget when it is disabled.
7 disabledforeground The foreground color of the widget when it is disabled.
8 fg The normal foreground color of the widget.
9 font The font type of the widget content.
10 format This option is used for the format string. It has no default value.
11 from_ It is used to show the starting range of the widget.
It is used to specify the justification of the multi-line widget content. The
12 justify
default is LEFT.
13 relief It is used to specify the type of the border. The default is SUNKEN.
This option is used to control the button auto repeat. The value is given in
14 repeatdelay
milliseconds.
15 repeatinterval It is similar to repeatdelay. The value is given in milliseconds.
It represents the state of the widget. The default is NORMAL. The possible
16 state
values are NORMAL, DISABLED, or "readonly".
It is like a control variable which is used to control the behaviour of the
17 textvariable
widget text.
It specify the maximum limit of the widget value. The other is specified by
18 to
the from_ option.
19 validate This option controls how the widget value is validated.
It is associated to the function callback which is used for the validation of
20 validatecommand
the widget content.
21 values It represents the tuple containing the values for this widget.
22 vcmd It is same as validation command.
23 width It represents the width of the widget.
24 wrap This option wraps up the up and down button the Spinbox.
This options is set to the set() method of scrollbar to make this widget
25 xscrollcommand
horizontally scrollable.
Methods
There are the following methods associated with the widget.
SN Option Description
delete(startindex, This method is used to delete the characters present at the specified
1
endindex) range.
2 get(startindex, endindex) It is used to get the characters present in the specified range.
3 identify(x, y) It is used to identify the widget's element within the specified range.
4 index(index) It is used to get the absolute value of the given index.
5 insert(index, string) This method is used to insert the string at the specified index.
6 invoke(element) It is used to invoke the callback associated with the widget.
Example
Output:
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
SN Option Description
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
2 bd the trough 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.
This option represents the distance
between the handle and the end of the
5 handlepad sash. For the 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
6 handlesize default size is 8 pixels. However, the
handle will always be a square.
It represents the height of the widget. If
we do not specify the height, it will be
7 height
calculated by the height of the child
window.
8 orient The orient will be set to
HORIZONTAL if we want to place the
child windows side by side. It can be set to
VERTICAL if we want to place the child
windows from top to bottom.
It represents the type of the border. The
9 relief
default is FLAT.
It represents the padding to be done
10 sashpad
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 sash. The
12 sashwidth
default is 2 pixels.
It is set to True to display the handles.
13 showhandle
The default value is false.
It represents the width of the widget. If
we don't specify the width of the widget,
14 Width
it will be calculated by the size of the
child widgets.
Methods
There are the following methods that are associated with the PanedWindow.
SN Method Description
1 add(child, options) It is used to add a window to the parent window.
2 get(startindex, endindex) This method is used to get the text present at the specified range.
3 config(options) It is used to configure the widget with the specified options.
Example
Output:
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
SN Option Description
1 bg The background color of the widget.
It represents the size of the border shown around the indicator. The default
2 bd
is 2 pixels.
3 Class The default value of the class is LabelFrame.
This option is used to specify which colomap is to be used for this widget.
By colormap, we mean the 256 colors that are used to form the graphics.
4 colormap
With this option, we can reuse the colormap of another window on this
widget.
If this is set to true, the LabelFrame becomes the container widget. The
5 container
default value is false.
It can be set to a cursor type, i.e. arrow, dot, etc. the mouse pointer is
6 cursor
changed to the cursor type when it is over the widget.
7 fg It represents the foreground color of the widget.
8 font It represents the font type of the widget text.
9 height It represents the height of the widget.
It represents the exact position of the text within the widget. The default is
10 labelAnchor
NW(north-west)
It represents the widget to be used for the label. The frame uses the text for
11 labelwidget
the label if no value specified.
The color of the focus highlight border when the widget doesn't have the
12 highlightbackground
focus.
13 highlightcolor The color of the focus highlight when the widget has the focus.
14 highlightthickness The width of the focus highlight border.
15 padx The horizontal padding of the widget.
16 pady The vertical padding of the widget.
17 relief It represents the border style. The default value is GROOVE.
18 text It represents the string containing the label text.
19 width It represents the width of the frame.
Example
Output:
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
Parameters
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.
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.
Example
Output:
2. showwarning()
This method is used to display the warning to the user. Consider the following example.
Example
Output:
3. showerror()
This method is used to display the error message to the user. Consider the following example.
Example
Output:
4. askquestion()
This method is used to ask some question to the user which can be answered in yes or no. Consider
the following example.
Example
Output:
5. askokcancel()
This method is used to confirm the user's action regarding some application activity. Consider the
following example.
Example
Output:
6. askyesno()
This method is used to ask the user about some action to which, the user can answer in yes or no.
Consider the following example.
Example
Output:
7. askretrycancel()
This method is used to ask the user about doing a particular task again or not. Consider the
following example.
Example
Output: