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