Tkinter filedialog to display Treeview

Displaying data in Treeview by using Tkinter filedialog to show file browser to upload & read file
We will use one Treeview to display the data of the Filedialog uploaded file ( after reading ) inside the same window.
Display data of uploaded file
import tkinter as tk
from tkinter import ttk,filedialog
from tkinter.filedialog import askopenfile
my_w = tk.Tk()
my_w.geometry("400x300")  # Size of the window 
my_w.title('www.plus2net.com')
my_font1=('times', 18, 'bold')
l1 = tk.Label(my_w,text='Upload File & read',width=30,font=my_font1)  
l1.grid(row=1,column=1)
b1 = tk.Button(my_w, text='Upload File', 
   width=20,command = lambda:upload_file())
b1.grid(row=2,column=1) 
trv=ttk.Treeview(my_w,selectmode='browse')
trv.grid(row=3,column=1,columnspan=4,padx=20,pady=20)
trv['show'] = 'tree'
def upload_file():
    file = filedialog.askopenfilename(
        filetypes=[("CSV files", ".csv")])
    if file:        
        fob=open(file,'r')    
        i=0
        for data in fob:
            trv.insert("",'end',iid=i,text=data)
            i=i+1
    else:
        print("No file chosen")
my_w.mainloop()  # Keep the window open

When user cancels the file window

After opening the file browser the user may use the cancel button to close the operation, to handle this we have added the if file: in above code. The else part of the code will display the message.

Tkitner Treeview to get Parent Child nodes and display data in hierarchical order


Tkinter filedialog.askopenfile() Upload and display image file
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate 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.



Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer