How it works...

To create a ttk.Treeview with multiple columns, we need to indicate the identifiers of each one with the columns option. Then, we can configure the header text by calling the heading() method.

We used identifiers #1, #2, and #3 since the first column, which contains the expandable icon and text, is always generated with the #0 identifier.

Also we passed the "headings" value to the show option to indicate that we want to hide the #0 column, because there will not be nested items.

The following values are valid for the show option:

  • "tree": Displays column #0
  • "headings": Displays the header row
  • "tree headings": Displays both column #0 and the header row—this is the default value
  • "": Does not display column #0 or the header row

After this, we attached a vertical scroll bar to our ttk.Treeview widget:

        columns = ("#1", "#2", "#3")
self.tree = ttk.Treeview(self, show="headings", columns=columns)
self.tree.heading("#1", text="Last name")
self.tree.heading("#2", text="First name")
self.tree.heading("#3", text="Email")
ysb = ttk.Scrollbar(self, orient=tk.VERTICAL, command=self.tree.yview)
self.tree.configure(yscroll=ysb.set)

To load the contacts into the table, we process the file with the reader() function from the csv module, and the row read in each iteration is added to ttk.Treeview.

This is done by calling the insert() method, which receives the parent node and the position to place the item.

Since all contacts are shown as top-level items, we pass an empty string as the first parameter and the END constant to indicate that each new item is inserted at the last position.

You can optionally provide some keyword arguments to the insert() method. Here, we specified the values option, which takes the sequence of values that is displayed in each column of the Treeview:

        with open("contacts.csv", newline="") as f:
for contact in csv.reader(f):
self.tree.insert("", tk.END, values=contact)
self.tree.bind("<<TreeviewSelect>>", self.print_selection)

The <<TreeviewSelect>> event is the virtual event generated when the user selects one or more items from the table. Within the print_selection() handler, we retrieve the current selection by calling the selection() method, and for each result, we will perform the following steps:

  1. With the item() method, we get the dictionary of options and values of the selected item
  2. We retrieve the first three values from the item dictionary, which correspond to the last name, first name, and email of the contact
  3. The values are formatted and printed into the standard output:
    def print_selection(self, event):
for selection in self.tree.selection():
item = self.tree.item(selection)
last_name, first_name, email = item["values"][0:3]
text = "Selection: {}, {} <{}>"
print(text.format(last_name, first_name, email))
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset