Address Book in Python using Tkinter

This article describes the process involved in making a Address Book, GUI based project with Python.

Unnati Shah
6 min readNov 30, 2020

Talking about the system, it contains all the required functions which include adding, viewing and updating the address records. While adding the address of a person, he/she has to provide first name, last name, street, city, state and the zip code. The user can also update the address if he/she wants to. It also discusses the overview of the project including the implementation of the source code and database connectivity using SQLite3, and then focuses on Graphic User Interface (GUI), describing the programming functions and libraries used in the design.

Problem Definition

The objective of this system is to validate users’ details and to successfully store it into the database. The system is designed in a Windows environment and written in Python 2.7.

Features:
1. Add address
2. First address
3. Next address
4. Previous address
5. Last address
6. Update address

I have used components of Tkinter module such as:

Tkinter Widgets:
1. Label
2. Entry
3. Button
4. Frame

Standard attributes & Geometry Management:
1. Dimensions
2. Fonts
3. Cursors
4. Grid

Modules

GUI Module

The GUI features for creating our interface are included in this module. There are a number of GUI frameworks in Python, but the only framework that is integrated into the standard Python library is Tkinter. There are many strengths to Tkinter. It’s cross-platform, so on Windows, MacOS and Linux, the same code works. Visual elements are made using elements of the native operating system, so Tkinter-built applications look like they belong on the platform where they are run. While the de-facto Python GUI system is considered by Tkinter, it’s not without criticism. One noteworthy criticism is that GUIs constructed with Tkinter look outdated. If you want a new shiny gui, Tkinter might not be what you’re looking for. However in contrast to other frameworks, Tkinter is lightweight and relatively painless to use.

I am using Tkinter as the main package for managing functionalities such as:

Creating a window:
The window is the foundation feature of the Tkinter GUI. Windows is the container where all the other elements of the GUI reside. These other GUI components are known as widgets, such as text boxes, labels, and buttons. There are widgets inside the windows.

Adding a Widget:
You can add a widget now that we’ve got a window. To add any text to a window, use class tk.Label. Build a label widget and assign it to a variable with the text “ “. There is no change in the window you built earlier. We just built the Label Widget, but we still haven’t added it to the window. There are many ways that widgets can be attached to a display. We’re using the .grid() Label widgets process. The bread and butter of the Python GUI system, Tkinter, are widgets. They are the components by which the software is dealt with by users. Each widget is class-defined in Tkinter. Here are the widgets that I have been using:

  1. Label: A widget used to display text on the screen
  2. Button: A button that can contain text and can perform an action when clicked
  3. Entry: A text entry widget that allows only a single line of text
  4. Frame: A rectangular region used to group related widgets or provide padding between widgets

Displaying clickable buttons with ‘Button’ widget:
In order to display clickable icons, button widgets are used. Every time they are pressed, they can be configured to call a function. Between Button and Label widgets, there are many similarities. A button is just a mark that you can press in several ways! For button widgets, the same keyword arguments you use to build and style a label will work.

Getting user input with ‘Entry’ widget:
It functions pretty much exactly like Label and Button widgets to build and style an Entry widget. The fascinating bit about Entry widgets, however is not how they can be styled. It’s how to get feedback from a user by using them. With Entry widgets, there are three key operations that you can perform:

  1. Retrieving text with .get()
  2. Deleting text with .delete()
  3. Inserting text with .insert()

Assigning widgets to frame with ‘Frame’ widget:
All of the widget forms we’ve used have a master attribute that’s set when we instantiate them — Mark, Icon, and Entry. That way, we can control the frame to which a widget is assigned. Frame widgets are useful for logically arranging other widgets. Similar widgets may be allocated to the same frame, so that the related widgets can remain together if the frame is ever shifted in the window. Frame widgets will add a little flare to your application’s visual appearance, in addition to logically grouping your widgets.

Controlling layout with Geometry Managers:

  1. The .place() Geometry Manager: To monitor the exact position that a widget should occupy in a window or frame, we use .place() Two keyword arguments, x and y, have to be given, defining the x- and y-coordinates for the widget’s top left corner. In pixels, both x and y are measured, not text units.
  2. The .grid() Geometry Manager: .grid() works by separating rows and columns into a window or frame. By calling .grid() and passing the row and column indices to the row and column keyword arguments, respectively, we define the position of the widget. The indices for both rows and columns start at 0, so a row index of 1 and a column index of 2 tells .grid() to put the widget in the third column of the second row.

You can change the padding of each cell in the grid to add some space around each frame. Padding is only a blank space surrounding and visually separating a widget from its contents.

Exterior and internal padding are the two types of padding. Any space around the outside of a grid cell is added through external padding. Two keyword arguments for .grid() are used to monitor it:

  1. padx adds padding in the horizontal direction.
  2. pady adds padding in the vertical direction.

By using the sticky parameter, you can change the position of each label within the grid cell. A string containing one or more of the following letters is approved by Sticky:

  1. “n” or “N” to align to the top-center part of the cell
  2. “e” or “E” to align to the right-center side of the cell
  3. “s” or “S” to align to the bottom-center part of the cell
  4. “w” or “W” to align to the left-center side of the cell

The letters “n”, “s”, “e”, and “w” come from the cardinal directions north, south, east, and west.

Using command:
Every Button widget has a command attribute that you can assign to a function. Whenever the button is pressed, the function is executed.

Functions Module

It contains all the required functions which include:

Add Address:
It contains all the functionalities related to storing the given record into existing database if the record is not already present.

Update Address:
It contains all the functionalities related to modifying the existing records in the database.

Previous Address:
It is used to fetch the previous record from the collection.

Next Address:
It is used to fetch the next record from the collection.

First Address:
It contains all the operations required to get the first record and display it to the user.

Last Address:
It contains all the operations required to get the last record and display it to the user.

Database

Schema

Image by Author

Data

Image by Author

Output

Image by Author

All the fields in the source code have been successfully tested as they uniquely feature this project.

You can find the Github Repository containing the source code for all the functions mentioned in this article here.

References:

[1] http://reydelaseo.cl/python-menu/python-gui.html

[2] https://www.coursef.com/python-3-tkinter-tutorial

[3] https://www.geeksforgeeks.org/create-first-gui-application-using-python-tkinter/

[4] https://devsday.ru/blog/details/5927

--

--

Unnati Shah

Data enthusiast, currently pursuing MS in Computer Science @ USC. For more information visit my website: https://unnatibshah.github.io/