Post on 26-Apr-2020
GUI Applications Using
Tkinter
By: Sanjeev Bhadauria PGT CS, KV BARABANKI
Blog: www.pythontrends.wordpress.com
YouTube Channel : “Python Trends”
Our Target to learn . . . .
Sanjeev Bhadauria, PGT CS, KV Barabanki
• 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.
Introduction to Tkinter
Sanjeev Bhadauria, PGT CS, KV Barabanki
• 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.
Tkinter
Sanjeev Bhadauria, PGT CS, KV Barabanki
• 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 be appeared after the execution of
above code.
This will be the main window’s
object.
Tkinter Widgets
Sanjeev Bhadauria, PGT CS, KV Barabanki
• Button
• Canvas
• Checkbutton
• Entry
• Frame
• Label
• Listbox
• Menubutton
• Menu
• Message
• Radiobutton
• Scale
• Scrollbar
• Text
• Toplevel
• Spinbox
• PanedWindow
• LabelFrame
• tkMessageBox
Standard Attributes
Sanjeev Bhadauria, PGT CS, KV Barabanki
• There are some common attributes of all the
widgets. They are -
• Dimensions
• Colors
• Fonts
• Anchors
• Relief styles
• Bitmaps
• Cursors
Geometry Management
Sanjeev Bhadauria, PGT CS, KV Barabanki
• 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.
Tkinter Button
Sanjeev Bhadauria, PGT CS, KV Barabanki
• 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( )
Tkinter Button
Sanjeev Bhadauria, PGT CS, KV Barabanki
Here we created a button object and make it visible in frame using pack ( ).
We called the function here which is to be executed after clicking on the button.
messagebox code.
Tkinter Label
Sanjeev Bhadauria, PGT CS, KV Barabanki
• 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. . . .
Tkinter Label
Sanjeev Bhadauria, PGT CS, KV Barabanki
Here we created a Label object and make it visible in frame using pack ( ).
Tkinter Frame
Sanjeev Bhadauria, PGT CS, KV Barabanki
• 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. . . .
Tkinter Frame
Sanjeev Bhadauria, PGT CS, KV Barabanki
Here three buttons are in same frame while one button “black” is in another frame
Tkinter Entry
Sanjeev Bhadauria, PGT CS, KV Barabanki
• 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. . . . .
Tkinter Entry
Sanjeev Bhadauria, PGT CS, KV Barabanki
Tkinter Canvas
Sanjeev Bhadauria, PGT CS, KV Barabanki
• 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
Tkinter Canvas
Sanjeev Bhadauria, PGT CS, KV Barabanki
Tkinter Checkbutton
Sanjeev Bhadauria, PGT CS, KV Barabanki
• 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. . . . .
Tkinter Checkbutton
Sanjeev Bhadauria, PGT CS, KV Barabanki
Tkinter Listbox
Sanjeev Bhadauria, PGT CS, KV Barabanki
• 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. . . . .
Tkinter Listbox
Sanjeev Bhadauria, PGT CS, KV Barabanki
Tkinter Menubutton
Sanjeev Bhadauria, PGT CS, KV Barabanki
• 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.
. . .
Tkinter Menubutton
Sanjeev Bhadauria, PGT CS, KV Barabanki
Tkinter Menu
Sanjeev Bhadauria, PGT CS, KV Barabanki
• 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( ),
Tkinter Menu
Sanjeev Bhadauria, PGT CS, KV Barabanki
tkimenu.py
Tkinter Radiobutton
Sanjeev Bhadauria, PGT CS, KV Barabanki
• 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.
Tkinter Radiobutton
Sanjeev Bhadauria, PGT CS, KV Barabanki
Tkinter Text
Sanjeev Bhadauria, PGT CS, KV Barabanki
• 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 . . . .
Tkinter Text
Sanjeev Bhadauria, PGT CS, KV Barabanki
Tkinter Spinbox
Sanjeev Bhadauria, PGT CS, KV Barabanki
• 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 . . . .
Tkinter Spinbox
Sanjeev Bhadauria, PGT CS, KV Barabanki
Student Entry Form
Sanjeev Bhadauria, PGT CS, KV Barabanki
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.
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
Sanjeev Bhadauria, PGT CS, KV Barabanki
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.
• Please follow our blog and subscribe our youtube
channel. So that you may get each and evry
chapters of python.
www.pythontrends.wordpress.com