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.
Tkinter filedialog.askopenfile()
Tkitner Treeview to get Parent Child nodes and display data in hierarchical order

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    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 FORUM . Contact us
    ©2000-2021 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer