Tkinter treeview widget (Python 3)
Show item hierarchy
How to use tkinter Treeview? The most typical is table and file directory traversal.
The TreeView widget is designed to show a hierarchy of items, with attributes next to eachother.
For instance, if you want to create an app that looks like the Windows File Explorer, you can do so with Tkinters TreeView widget. For more examples follow this link.
Related course: Python Desktop Apps with Tkinter
tkinter treeview widget
directory
The program below uses the tkinter treeview to create a directory view. It shows the icons directory on Linux, if you use Mac or Windows change the directory to one that exists on your system.
import os
import tkinter as tk
from tkinter import ttk
root=tk.Tk()
root.geometry('320x240')
f=tk.Frame(root)
tv=ttk.Treeview(f,show='tree')
ybar=tk.Scrollbar(f,orient=tk.VERTICAL,
command=tv.yview)
tv.configure(yscroll=ybar.set)
directory='/usr/share/icons/'
tv.heading('#0',text='Dir:'+directory,anchor='w')
path=os.path.abspath(directory)
node=tv.insert('','end',text=path,open=True)
def traverse_dir(parent,path):
for d in os.listdir(path):
full_path=os.path.join(path,d)
isdir = os.path.isdir(full_path)
id=tv.insert(parent,'end',text=d,open=False)
if isdir:
traverse_dir(id,full_path)
traverse_dir(node,path)
ybar.pack(side=tk.RIGHT,fill=tk.Y)
tv.pack()
f.pack()
root.mainloop()
Description.
Steps for traversing the directory.
Step 1: Create a Frame
root=tk.Tk()
root.geometry('320x240')
f=tk.Frame(root)
Step 2: Create a Treeview. show='tree'
, indicating a tree structure.
tv=ttk.Treeview(f,show='tree')
Step 3: Create a vertical scroll bar. When there are many files or directories, the scrolling display
ybar=tk.Scrollbar(f,orient=tk.VERTICAL,
command=tv.yview)
tv.configure(yscroll=ybar.set)
Step 4: Setting up the first layer nodes
.heading('#0',text='directory:'+directory,anchor='w')
Step 5: Insert a file or directory in this directory
Step 6: Recursively traverses all subdirectories and inserts them into Treeview
table
This example is a virtual trading company sales record. It is divided into 6 sales regions in North Europe, East Europe, South Europe, Western Europe and others. The products are divided into 4 parts of electronic products, cosmetics, clothing and daily necessities.
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.geometry('320x240')
tk.Label(root,text='tkinter treeview widget').pack()
area=('#', 'Northern Europe', 'Eastern Europe', 'Southern Europe', 'Western Europe', 'USA', 'Canada', 'Australia')
ac=('all','n','e','s','ne','nw','sw','c')
sales_data=[('Electronics','47814','41443','4114','14314','413','14314','84387'),
('Cosmetics','48349','94734', '4743','43434','43844','43843','88844'),
('Clothing','14841','49761', '147471','49094' ,'57844', '48499','49494'),
('Misc','4939','43934','43993','6894', '39933','3903','4344')
]
tv=ttk.Treeview(root,columns=ac,show='headings',height=7)
for i in range(8):
tv.column(ac[i],width=70,anchor='e')
tv.heading(ac[i],text=area[i])
tv.pack()
for i in range(4):
tv.insert('','end',values=sales_data[i])
root.mainloop()
Description: Steps to create the table.
Step 1: Create Treeview
tv=ttk.Treeview(root,columns=ac,show='headings',height=7)
columns set the name of the column, show='headings' means the first row is a table header, height=7 means the height of 7 rows is displayed
Step 2: Set column properties
tv.column(ac[i],width=70,anchor='e')
width is the width that defines each column. It is 70 in the code. if you need a different width for each column. a list can be used to set the column width for each column. anchor is the way to define the alignment.
Step 3: Set the table header name
tv.heading(ac[i],text=area[i])
text is what is displayed in the table header.
Step 4: Insert data
tv.insert('','end',values=sales_data[i])
'end' means to insert new content after the last line. values are the data to be inserted, entered in a list or tuple.
Directory traversal
The directory traversal uses Treeview's tree structure (show='tree'). A recursive method is used to traverse the specified directory.