Populating the Treeview

Now that we have our Treeview widget, we'll create a populate() method to populate it with data:

    def populate(self, rows):
        """Clear the treeview & write the supplied data rows to it."""

The rows argument will take a list of the dict data types, such as what is returned from model. The idea is that the controller will fetch a list from the model and then pass it to this method.

Before refilling Treeview, we need to empty it:

        for row in self.treeview.get_children():
            self.treeview.delete(row)

The   get_children() method of Treeview returns a list of every row's iid. We're iterating this list, passing each iid to the Treeview.delete() method, which, as you'd expect, deletes the row.

With the Treeview cleared, we can ...

Get Python GUI Programming with Tkinter now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.