my_select
: Show the file dialog box to select a directory.my_treeview
: Create the Structure of the Treeview without any rows.my_details
: Reads the file details of the selected directory and creates the DataFrame. my_upd
: Add rows to Treeview from the DataFramemy_sort
: Sort the DataFrame based on user click and call my_upd() to refresh the view. def my_select():
my_dir = filedialog.askdirectory() # select directory
if my_dir: # user has not cancelled the dialog box
l1.config(text=my_dir) # update Label with directory path
my_treeview() # Create the Treeview structure
my_details(my_dir) # Collect file details of selected path
def my_treeview():
global trv
trv = ttk.Treeview(my_w, selectmode ='browse',show='headings')
trv.grid(row=2,column=1,columnspan=3,padx=30,pady=10)
# column identifiers
trv["columns"] = ("1", "2","3","4")
trv.column("1", width = 150, anchor ='w')
trv.column("2", width = 100, anchor ='c')
trv.column("3", width = 100, anchor ='c')
trv.column("4", width = 70, anchor ='c')
trv.heading(1, text ="Name",anchor='w',command=lambda :my_sort('Name'))
trv.heading(2, text ="Type",anchor='c',command=lambda :my_sort('Type'))
trv.heading(3, text ="Date Modified",anchor='c',command=lambda :my_sort('Date Modified'))
trv.heading(4, text ="Size",anchor='c',command=lambda :my_sort('Size'))
def my_details(path):
global trv,df
#path = "E:\\testing\\images\\test3.png" # Directory Path
df.drop(df.index,inplace=True) # remove all rows of the DataFrame
files=os.listdir(path) # List of files and dirs. of the path
for f in files: # Looping through all files and dirs
f_path=path+'\\'+f # Path with file name
t_stamp=os.path.getmtime(f_path) # for file modificaton time
#t_stamp=os.path.getctime(path) # for file Creation time
f_name,f_extension=os.path.splitext(f_path) # get file extension
size=os.path.getsize(f_path) # size of file in bytes
dt_mod = datetime.fromtimestamp(t_stamp) # date object
#print('File Modified on:', dt_mod) # Prting date and time
m_date = datetime.strftime(dt_mod, '%Y-%m-%d') # Change format
#print(f, f_extension, m_date,size)
df_new=[f,f_extension,m_date,size] # List with all details
df.loc[len(df)]=df_new # add list as new row to DataFrame
my_upd() # Call to show Treeview using the DataFrame
def my_upd():
global trv,df
trv.delete(*trv.get_children())# delete all rows from treeview
r_set = df.to_numpy().tolist() # Create list of list using rows
for dt in r_set:
trv.insert("", "end", values=dt) # adding row to TreeView
# Adding vertical scrollbar
vsb = ttk.Scrollbar(my_w, orient="vertical", command=trv.yview)
vsb.grid(row=2, column=4, sticky="ns")
trv.configure(yscrollcommand=vsb.set)
def my_sort(col_name): # Update the dataframe after sorting
global df,order
if order:
order=False # set ascending value
else:
order=True
df=df.sort_values(by=[col_name],ascending=order) # sort values
my_upd() # refresh the Treeview
from tkinter import ttk
import tkinter as tk
from tkinter import filedialog
import os
from datetime import datetime
import pandas as pd
# Creating tkinter my_w
my_w = tk.Tk()
my_w.geometry("550x380") # width x height of the window
my_w.title("www.plus2net.com")
b1=tk.Button(my_w,text='Select directory',font=22,
command=lambda:my_select(),bg='lightgreen')
b1.grid(row=1,column=1,padx=10,pady=10)
l1=tk.Label(my_w,text='',bg='yellow',font=18)
l1.grid(row=1,column=2,padx=2,columnspan=2)
df = pd.DataFrame(columns=['Name','Type','Date Modified','Size']) # blank DataFrame
order=True
def my_select():
my_dir = filedialog.askdirectory() # select directory
if my_dir: # user has not cancelled the dialog box
l1.config(text=my_dir) # update Label with directory path
my_treeview() # Create the Treeview structure
my_details(my_dir) # Collect file details of selected path
# Using treeview widget
def my_treeview():
global trv
trv = ttk.Treeview(my_w, selectmode ='browse',show='headings')
trv.grid(row=2,column=1,columnspan=3,padx=30,pady=10)
# column identifiers
trv["columns"] = ("1", "2","3","4")
trv.column("1", width = 150, anchor ='w')
trv.column("2", width = 100, anchor ='c')
trv.column("3", width = 100, anchor ='c')
trv.column("4", width = 70, anchor ='c')
trv.heading(1, text ="Name",anchor='w',command=lambda :my_sort('Name'))
trv.heading(2, text ="Type",anchor='c',command=lambda :my_sort('Type'))
trv.heading(3, text ="Date Modified",anchor='c',command=lambda :my_sort('Date Modified'))
trv.heading(4, text ="Size",anchor='c',command=lambda :my_sort('Size'))
def my_details(path):
global trv,df
#path = "E:\\testing\\images\\test3.png" # Directory Path
df.drop(df.index,inplace=True) # remove all rows of the DataFrame
files=os.listdir(path) # List of files and dirs. of the path
for f in files: # Looping through all files and dirs
f_path=path+'\\'+f # Path with file name
t_stamp=os.path.getmtime(f_path) # for file modificaton time
#t_stamp=os.path.getctime(path) # for file Creation time
f_name,f_extension=os.path.splitext(f_path) # get file extension
size=os.path.getsize(f_path) # size of file in bytes
dt_mod = datetime.fromtimestamp(t_stamp) # date object
#print('File Modified on:', dt_mod) # Prting date and time
m_date = datetime.strftime(dt_mod, '%Y-%m-%d') # Change format
#print(f, f_extension, m_date,size)
df_new=[f,f_extension,m_date,size] # List with all details
df.loc[len(df)]=df_new # add list as new row to DataFrame
my_upd() # Call to show Treeview using the DataFrame
def my_upd():
global trv,df
trv.delete(*trv.get_children())# delete all rows from treeview
r_set = df.to_numpy().tolist() # Create list of list using rows
for dt in r_set:
trv.insert("", "end", values=dt) # adding row to TreeView
# Adding vertical scrollbar
vsb = ttk.Scrollbar(my_w, orient="vertical", command=trv.yview)
vsb.grid(row=2, column=4, sticky="ns")
trv.configure(yscrollcommand=vsb.set)
def my_sort(col_name): # Update the dataframe after sorting
global df,order
if order:
order=False # set ascending value
else:
order=True
df=df.sort_values(by=[col_name],ascending=order) # sort values
my_upd() # refresh the Treeview
my_w.mainloop()
Part I : Searching DataFrame and displaying result in Treeview
Part II : Selection of row in Treeview Part III : Selection Columns of DataFrame using CheckButtons Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.