Reader small image

You're reading from  Python GUI Programming with Tkinter, 2nd edition - Second Edition

Product typeBook
Published inOct 2021
Reading LevelBeginner
PublisherPackt
ISBN-139781801815925
Edition2nd Edition
Languages
Tools
Right arrow
Author (1)
Alan D. Moore
Alan D. Moore
author image
Alan D. Moore

Alan D. Moore is a data analyst and software developer who has been solving problems with Python since 2006. He's developed both open source and private code using frameworks like Django, Flask, Qt, and of course Tkinter, and is known to contribute to various open-source Python and JavaScript projects. Alan maintains a YouTube channel, “Alan D Moore Codes”, where he posts Python, PyQt, and Tkinter tutorials. Alan lives in Franklin, Tennessee, where he works for the County Government, and with his wife Cara raises a crew of children who are just as geeky as their dad.
Read more about Alan D. Moore

Right arrow

Implementing read and update in the model

Our entire design up to this point has been centered around a form that only appends data to a file; adding read and update capabilities is a fundamental change that will touch nearly every portion of the application.

It may seem like a daunting task, but by taking it one component at a time, we'll see that the changes are not so overwhelming.

The first thing we should do is update our documentation. Open the abq_data_entry_spec.rst file in the docs folder, and let's start with the Requirements section:

Functional Requirements:
  * Provide a UI for reading, updating, and appending 
    data to the CSV file
  * ...

And, of course, we should also update the part that is not required, like so:

The program does not need to:
  * Allow deletion of data.

Now, it's a simple matter of making the code match with the documentation. Let's get started!

Adding read and update to the CSVModel class

Take...

The Ttk Treeview

For users to be able to view the contents of a CSV file and select records to edit, we'll need to implement a new view in the application capable of displaying tabular data. This record list view will allow our users to browse the content of the file and open records for viewing or editing.

Our users are accustomed to seeing this data in a spreadsheet, laid out in a table-like format, so it makes sense to design our view in a similar fashion.

For building table-like views with selectable rows, Tkinter gives us the Ttk Treeview widget. To build our record list view, we'll need to learn about Treeview.

Anatomy of a Treeview

To help us explore the treeview, let's go through a few basic terms and concepts related to the widget. A treeview is designed to display hierarchical data; that is, data that is organized into nodes, where each node can have exactly one parent node and zero or more child nodes. The following diagram shows an example...

Implementing a record list with Treeview

Now that we understand how to use the Treeview widget, it's time to implement a GUI that will allow us to browse the records in the CSV file and open them for editing. Let's take a moment to plan out what it is that we need to create:

  • We want to lay out the CSV data in a table structure, similar to how it would look in a spreadsheet. This will be a flat table, not a hierarchy.
  • Each table row will represent a record in the file. When a user double-clicks the row, or highlights it and presses Enter, we want the record form to open with the selected record.
  • We don't really need to show every field in the table, since its purpose is merely to locate records for editing. Instead, we'll show only the rows that uniquely identify a record to the user. Namely, those are Date, Time, Lab, and Plot. We can also show the CSV row number.
  • There isn't really a need to sort the data, so we won't implement...

Adding the record list to the application

Now that we have a model capable of reading and updating data, and a RecordList widget capable of displaying the contents of a file, we need to make changes to the rest of the application to enable everything to work together. Specifically, we'll have to do the following:

  • We'll need to update the DataRecordForm to be suitable for updating existing records as well as adding new ones.
  • We'll need to update the layout of the Application window to accommodate the new record list.
  • We'll need to create new Application callbacks to handle loading records and navigating the application.
  • Finally, we'll need to update the main menu with new options for the added functionality.

Let's get started!

Modifying the record form for read and update

As long as we're still in views.py, let's scroll up to look at our DataRecordForm class and adjust it to make it capable of loading...

Summary

We have changed our program from being an append-only data entry form to an application capable of loading, viewing, and updating data from existing files. In the process, you learned how to update our model so that it could read and update CSV files. You also explored the Treeview widget, including its basic use, virtual events, and column callbacks. You explored using the Treeview widget with hierarchical data structures by creating a file-browsing tool. You learned how to organize multi-form applications using a Notebook widget, and how to create scrolling interfaces using the Scrollbar widget. Finally, you integrated these concepts into the ABQ Data Entry application to address user needs.

In our next chapter, we'll be learning how to modify the look and feel of our application. We'll learn about using widget attributes, styles, and themes, as well as working with bitmapped graphics.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Python GUI Programming with Tkinter, 2nd edition - Second Edition
Published in: Oct 2021Publisher: PacktISBN-13: 9781801815925
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Alan D. Moore

Alan D. Moore is a data analyst and software developer who has been solving problems with Python since 2006. He's developed both open source and private code using frameworks like Django, Flask, Qt, and of course Tkinter, and is known to contribute to various open-source Python and JavaScript projects. Alan maintains a YouTube channel, “Alan D Moore Codes”, where he posts Python, PyQt, and Tkinter tutorials. Alan lives in Franklin, Tennessee, where he works for the County Government, and with his wife Cara raises a crew of children who are just as geeky as their dad.
Read more about Alan D. Moore