B.Sc. Hons.
Computing/Information System
ICT 1405 - Programming Methodologies
8 - Tkinter
<@Lanka Nippon BizTech Institute> 1
ACKNOWLEDGEMENT
Presented by : Ms. C. D. Padmaperuma
Ms. Hansika Samanthi
Contributors : Ms. Hansika Samanthi
Ms. C. D. Padmaperuma
Programming Methodologies | Tkinter 2
REFERENCES
• Course Page
UOG 6: https://lms.lnbti.lk/course/view.php?id=851
UOG 11: https://lms.lnbti.lk/course/view.php?id=1306
• References available for the course
w3schools Python - https://www.w3schools.com/Python/
Real Python - https://realpython.com/
Programming Methodologies | Tkinter 3
GUI
Graphical User Interface
• A system of interactive visual
components for a computer or system
software is called a GUI (graphical
user interface).
• GU4I is the interface that uses
graphical elements to let people
interact as per requirement with
electronic devices.
Programming Methodologies | Tkinter 4
GUI Programming
• GUI programming, or Graphical User Interface programming, involves creating
applications that allow users to interact with software through graphical
elements such as windows, buttons, text fields, and icons, rather than through
text-based commands or text interfaces.
Programming Methodologies | Tkinter 5
Tkinter?
• Tkinter is the standard GUI (Graphical User Interface) library for Python.
• It provides a fast and easy way to create simple yet functional GUIs for your
Python applications.
• Tkinter comes bundled with Python, no need to install it separately if Python is
installed on the system.
Programming Methodologies | Tkinter 6
• Ease of Use:
• Tkinter is straightforward to use, especially
for beginners.
• It provides a simple way to create windows,
dialogs, buttons, and other GUI elements.
• Cross-Platform:
• Tkinter applications can run on Windows,
Why Use Tkinter? macOS, and Linux without modification.
• Rich Set of Widgets:
• Tkinter comes with a variety of widgets like
buttons, labels, text boxes, checkboxes, and
more.
• Community and Documentation:
• Being the standard GUI library for Python, it
has a large community and plenty of
documentation and tutorials available.
Programming Methodologies | Tkinter 7
• Tkinter is the only framework that’s built into the Python standard
Tkinter library.
• It’s cross-platform, so the same code works on Windows, macOS,
and Linux.
To create a Tkinter Python app, you follow
these basic steps:
1. Import the tkinter module:
2. Create the main window (container):
The main window (root) serves as the
container for all the GUI elements.
Note that in Python 2.x, the module is named
‘Tkinter’, while in Python 3.x, it is named
‘tkinter’.
Programming Methodologies | Tkinter 8
3. Add widgets to the main window:
• You can add any number of widgets like buttons, labels, entry fields,
etc., to the main window to design the interface as desired.
4. Apply event triggers to the widgets:
• You can attach event triggers to the widgets to define how they respond
to user interactions.
Programming Methodologies | Tkinter 9
Layout Management
• In Tkinter, layout management is essential for positioning and organizing
widgets within a window.
• Tkinter provides three main geometry managers for layout: pack, grid, and
place.
• Each of these has its own way of organizing widgets and is suited for different
types of layout needs.
Programming Methodologies | Tkinter 10
Pack Geometry Manager
• The pack geometry manager organizes
widgets in blocks before placing them in the
parent widget.
• It works well for simple layouts where you
want widgets to be stacked either vertically
or horizontally.
Programming Methodologies | Tkinter 11
• Key Options:
• side:
• Determines which side of the parent widget the child widget is packed
against (e.g., TOP, BOTTOM, LEFT, RIGHT).
• fill:
• Determines how the widget should expand to fill the available space
(NONE, X, Y, BOTH).
• expand:
• A boolean that specifies whether the widget should expand to fill any
extra space in the parent widget.
Programming Methodologies | Tkinter 12
Programming Methodologies | Tkinter 13
Grid Geometry Manager
• The grid geometry manager organizes widgets in
a table-like structure of rows and columns.
• It is more flexible for complex layouts involving
multiple rows and columns.
Programming Methodologies | Tkinter 14
• Key Options:
• row:
• The row in which the widget is placed.
• column:
• The column in which the widget is placed.
• rowspan:
• The number of rows the widget should span.
• columnspan:
• The number of columns the widget should span.
• sticky:
• Determines where the widget should be placed within its cell (e.g., N, E,
S, W, NE).
Programming Methodologies | Tkinter 15
Programming Methodologies | Tkinter 16
Place Geometry Manager
• The place geometry manager allows for
precise positioning of widgets by specifying
their exact coordinates or relative positions
within the parent widget.
• It provides more control over widget
placement but is generally less flexible for
dynamic layouts.
Programming Methodologies | Tkinter 17
• Key Options:
• x, y:
• The absolute coordinates where the widget is placed.
• relx, rely:
• The relative coordinates (0.0 to 1.0) for placement within the parent widget.
• anchor:
• Specifies which part of the widget should be placed at the coordinates (e.g.,
nw, center, se).
Programming Methodologies | Tkinter 18
Programming Methodologies | Tkinter 19
Tkinter Widgets
• Tkinter provides various controls, such as
buttons, labels and text boxes used in a
GUI application.
• These controls are commonly called
widgets.
• There are many types of widgets in Tkinter.
Programming Methodologies | Tkinter 20
Frame
• Frame is a container widget that can be used to group and organize other
widgets within a window.
• It helps in structuring the layout of the interface by acting as a parent widget for
other widgets like buttons, labels, entry boxes, and even other frames.
• Frames are useful for creating more complex layouts and for grouping widgets
logically.
Programming Methodologies | Tkinter 21
Programming Methodologies | Tkinter 22
Label
• The Label widget is used to display text or images.
• Parameters:
• text: The text to be displayed.
• font: The font of the text.
• bg: Background color.
• fg: Foreground (text) color.
Programming Methodologies | Tkinter 23
Programming Methodologies | Tkinter 24
Button
• The Button widget is used to create a clickable button.
• Parameters:
• text:
• The text on the button.
• command:
• The function to be called when the button is clicked.
Programming Methodologies | Tkinter 25
Programming Methodologies | Tkinter 26
Entry
• The Entry widget is used to create a single-line text input field.
• Parameters:
• show:
• Used to mask the input (useful for passwords).
• width:
• The width of the entry widget.
Programming Methodologies | Tkinter 27
Programming Methodologies | Tkinter 28
Text
• The Text widget is used to create a multi-line text input field.
• Parameters:
• height:
• The height of the text widget.
• width:
• The width of the text widget.
Programming Methodologies | Tkinter 29
Programming Methodologies | Tkinter 30
Checkbutton
• The Checkbutton widget is used to create a checkbox.
• Parameters:
• text:
• The text next to the checkbox.
• variable:
• The variable that holds the state of the checkbox.
Programming Methodologies | Tkinter 31
Programming Methodologies | Tkinter 32
Radiobutton
• The Radiobutton widget is used to create a set of mutually exclusive radio
buttons.
• Parameters:
• text:
• The text next to the radio button.
• variable:
• The variable that holds the state of the selected radio button.
• value:
• The value associated with the radio button.
Programming Methodologies | Tkinter 33
Programming Methodologies | Tkinter 34
Activity 1
Programming Methodologies | Tkinter 35