Configuring a Treeview widget

Now, let's create our Treeview widget by executing the following code snippet:

        self.treeview = ttk.Treeview(self,
            columns=list(self.column_defs.keys())[1:],
            selectmode='browse')

Note that we're excluding the #0 column from our columns list; it should never be specified here since it's automatically created. We're also choosing the browse select mode, so that users can select individual rows of the CSV file.

Let's go ahead and add our Treeview widget to RecordList and make it fill the widget:

        self.columnconfigure(0, weight=1)
        self.rowconfigure(0, weight=1)
        self.treeview.grid(row=0, column=0, sticky='NSEW')

Now, configure the columns and headings of Treeview by iterating through the column_defs dictionary:

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.