GUI Applications Using
Tkinter
By: Sanjeev Bhadauria
PGT CS, KV BARABANKI
Blog: www.pythontrends.wordpress.com
YouTube Channel : “Python Trends”
Our Target to learn . . . .
• In this session we will learn the basics of the
tkinter.
• We will learn about the widgets of tkinter.
• We will learn the coding system of widgets.
• We will learn the properties of different
widgets of tkinter which are usefull in making
our project.
• We will make a GUI application using tkinter
and database connectivity.
Sanjeev Bhadauria, PGT CS, KV Barabanki
Introduction to Tkinter
• Tkinter is a kind of python interface to develop
Graphical User Interface type application .
• It contains various tools to create such types of
applications.
• Tkinter is the standard GUI Library for Python.
• When we use Python and Tkinter to develop GUI
applications it makes the task very easy.
• Tkinter provides a powerful object-oriented interface
to the Tk GUI Kit.
Sanjeev Bhadauria, PGT CS, KV Barabanki
Tkinter
• To use Tkinter we have to do following steps –
1. Import tkinter module
2. Create the GUI application main window.
3. Add two or more widgets(controls) to the GUI application.
4. Enter the main event loop to take action against each
event triggered by the user. eg.
This window will
This will be be appeared after
the main the execution of
window’s above code.
object.
Sanjeev Bhadauria, PGT CS, KV Barabanki
Tkinter Widgets
• Button • Radiobutton
• Canvas • Scale
• Checkbutton • Scrollbar
• Entry • Text
• Frame • Toplevel
• Label • Spinbox
• Listbox • PanedWindow
• Menubutton • LabelFrame
• Menu • tkMessageBox
• Message
Sanjeev Bhadauria, PGT CS, KV Barabanki
Standard Attributes
• There are some common attributes of all the
widgets. They are -
• Dimensions
• Colors
• Fonts
• Anchors
• Relief styles
• Bitmaps
• Cursors
Sanjeev Bhadauria, PGT CS, KV Barabanki
Geometry Management
• All Tkinter widgets have access to specific
geometry management methods, which organizes
the widgets throughout the parent widget area. For
this purpose it provides following manager classes –
• The pack ( ) method : it manages the widgets in blocks.
• The grid ( ) method : it manages the widgets in table like
structure.
• The place ( ) method : it manages the widgets in in a
specific position in the parent widget.
Sanjeev Bhadauria, PGT CS, KV Barabanki
Tkinter Button
• This widget is used to add buttons ina Python
application. Its syntax is -
w = Button (top, option = value . . . . . )
Here arguments –
top – is the parent widget.
option – has many attributes to set the button
properties like activebackground, activeforeground, bd,
bg, command, font, height, highlinghcolor, image,
justify, padx, pady, relief, width, etc. . . .
Some commonly used methods for this widget are –
flash( ) and invoke( )
Sanjeev Bhadauria, PGT CS, KV Barabanki
Tkinter Button
messagebox code.
Here we created a We called the
button object and function here which
make it visible in is to be executed
frame using pack ( ). after clicking on the
button.
Sanjeev Bhadauria, PGT CS, KV Barabanki
Tkinter Label
• This widget is used to display the text or images.
Here the text may be updated at any time you want.
w = Label(top, option, . . . . . )
Here arguments –
top – is the parent widget.
option – has many attributes to set the properties like
anchor,bg, bitmap, bd, cursor, font, fg, height, image,
justify, padx, pady, text, textvariable, underline, width,
wraplength, etc. . . .
Sanjeev Bhadauria, PGT CS, KV Barabanki
Tkinter Label
Here we created a
Label object and
make it visible in
frame using pack ( ).
Sanjeev Bhadauria, PGT CS, KV Barabanki
Tkinter Frame
• This widget is very important for the process of
grouping and organizing other widgets. It is like a
container.
w = Frame(top, option, . . . . . )
Here arguments –
top – is the parent widget.
option – has many attributes to set the properties like
bg, bd, cursor, height, highlightbackground,
highlightcolor, highlightthikness, relief, width etc. . . .
Sanjeev Bhadauria, PGT CS, KV Barabanki
Tkinter Frame
Here three buttons are
in same frame while
one button “black” is in
another frame
Sanjeev Bhadauria, PGT CS, KV Barabanki
Tkinter Entry
• It is used to input a single line text entry from the
user. The general syntax is -
w = Entry(top, option, . . . . . )
Here arguments –
top – is the parent widget.
option – has many attributes to set the properties like
bg, bd, cursor, command, font, exportselection, fg,
highlightcolor, justify, relief, selectbackground,
selectborderwidth, show state, taxtvariable, width,
xscrollcommand, etc. . . .
Methods : delete( ), get(), icursor( ), index( ), insert( ),
etc. . . . .
Sanjeev Bhadauria, PGT CS, KV Barabanki
Tkinter Entry
Sanjeev Bhadauria, PGT CS, KV Barabanki
Tkinter Canvas
• It is used to draw pictures and other complex layout
like graphics, text, widgets etc. The general syntax
is -
w = Canvas(top, option, . . . . . )
Here arguments –
top – is the parent widget.
option – has many attributes to set the properties like
bg, bd, cursor, highlightcolor, width, height, etc. . . .
Methods : delete( ), get(), icursor( ), index( ), insert( ),
etc. . . . .
It also supports arc, line, image, oval, polygon
Sanjeev Bhadauria, PGT CS, KV Barabanki
Tkinter Canvas
Sanjeev Bhadauria, PGT CS, KV Barabanki
Tkinter Checkbutton
• It is used to display number of options to a user as
toggle buttons, from which user can select one or
more options. The general syntax is -
w = Checkbutton(top, option, . . . . . )
Here arguments –
top – is the parent widget.
option – has many attributes to set the properties like
bg, activebackground, activeforeground, bitmap, bd,
command cursor, disabledforeground, font, fg, etc. . . .
Methods : deselect( ), flash(), invoke( ), select( ),
toggle( ), etc. . . . .
Sanjeev Bhadauria, PGT CS, KV Barabanki
Tkinter Checkbutton
Sanjeev Bhadauria, PGT CS, KV Barabanki
Tkinter Listbox
• The Listbox widget is used to display a list of items
from which a user can select a number of items.
The general syntax is -
w = Listbox(top, option, . . . . . )
Here arguments –
top – is the parent widget.
option – has many attributes to set the properties like
bg, bd, cursor, font, fg, height, selectmode, etc. . . .
Methods : active( ), curselection(), delete( ), get( ),
index( ), insert( ), nearest( ), see( ), size( ), etc. . . . .
Sanjeev Bhadauria, PGT CS, KV Barabanki
Tkinter Listbox
Sanjeev Bhadauria, PGT CS, KV Barabanki
Tkinter Menubutton
• A menubutton is the part of a drop-down menu that
stays on the screen all the time. Every menubutton
is associated with a Menu widget that can display
the choices for that menubutton when the user
clicks on it. The general syntax is -
w = Menubutton(top, option, . . . . . )
Here arguments –
top – is the parent widget.
option – has many attributes to set the properties like
bg, anchorbd, bitmap, cursor, direction, fg, height,
image, justifymenu, padx, pady, text, textvariable, etc.
.. .
Sanjeev Bhadauria, PGT CS, KV Barabanki
Tkinter Menubutton
Sanjeev Bhadauria, PGT CS, KV Barabanki
Tkinter Menu
• The goal of this widget is to allow us to create all kinds of menus that can
be used by our applications. The core functionality provides ways to
create three menu types: pop-up, toplevel and pull-down.
• It is also possible to use other extended widgets to implement new types
of menus, such as the OptionMenu widget, which implements a special
type that generates a pop-up list of items within a selection
• . The general syntax is -
w = Menu (top, option, . . . . . )
Here arguments –
top – is the parent widget.
option – has many attributes to set the properties like bg, anchorbd, bitmap,
cursor, direction, fg, height, image, justifymenu, padx, pady, text, textvariable,
etc. . .
methods– add_command( ), add_radiobutton( ), add_checkbutton( ),
add_cascade( ), add_separator( ), add( ), delete( ), entryconfig( ), index( ),
invoke( ), type( ),
Sanjeev Bhadauria, PGT CS, KV Barabanki
Tkinter Menu
tkimenu.py
Sanjeev Bhadauria, PGT CS, KV Barabanki
Tkinter Radiobutton
• This widget implements a multiple-choice button, which is a way to
offer many possible selections to the user and lets user choose
only one of them.
• In order to implement this functionality, each group of radiobuttons
must be associated to the same variable and each one of the
buttons must symbolize a single value. You can use the Tab key to
switch from one radionbutton to another
• . The general syntax is -
w = Radiobutton(top, option, . . . . . )
Here arguments –
top – is the parent widget.
option – has many attributes to set the properties.
Sanjeev Bhadauria, PGT CS, KV Barabanki
Tkinter Radiobutton
Sanjeev Bhadauria, PGT CS, KV Barabanki
Tkinter Text
• Text widgets provide advanced capabilities that allow you to edit a
multiline text and format the way it has to be displayed, such as
changing its color and font.
• . The general syntax is -
w = Text(top, option, . . . . . )
Here arguments –
top – is the parent widget.
option – has many attributes to set the properties like bg, bd, cursor,
exportselection, font, fg, height, highlightcolor, padx, pady, relief,
selectbackground, tabs, state, spacing1, width, wrap,
xscrollcommand, yscrollcommand etc….
Methods – delete( ) , get( ), insert( ) , see( ), index( ) etc . . . .
Sanjeev Bhadauria, PGT CS, KV Barabanki
Tkinter Text
Sanjeev Bhadauria, PGT CS, KV Barabanki
Tkinter Spinbox
• The Spinbox widget is a variant of the standard Tkinter Entry
widget, which can be used to select from a fixed number of values.
• . The general syntax is -
w = Spinbox(top, option, . . . . . )
Here arguments –
top – is the parent widget.
option – has many attributes to set the properties like bg, bd, cursor,
command, fg, font, format, justify, state, from, to, validate, wrap
etc….
Methods – delete( ) , get( ), insert( ) , identify( ), index( ), invoke( )
etc . . . .
Sanjeev Bhadauria, PGT CS, KV Barabanki
Tkinter Spinbox
Sanjeev Bhadauria, PGT CS, KV Barabanki
Student Entry Form
We will make an entry form using tkinter. And the data
entered into that form will be saved in the following
table in MySQL. (initially the table is empty.)
Form will be like this.
Sanjeev Bhadauria, PGT CS, KV Barabanki
Code (File Name LoginForm.py
Sanjeev Bhadauria, PGT CS, KV Barabanki
Code
Sanjeev Bhadauria, PGT CS, KV Barabanki
Code
Sanjeev Bhadauria, PGT CS, KV Barabanki
Output
When you click SUBMIT Button the data
entered in the text fields will be saved to
database which can be viewed using MySQL.
We can verify MySQL
whether the data is
enterd or not.
Sanjeev Bhadauria, PGT CS, KV Barabanki
• Please follow our blog and subscribe our youtube
channel. So that you may get each and evry
chapters of python.
www.pythontrends.wordpress.com