0

I am creating a program that let me visualise my csv file with tkinter. However, I am not able to read the file I grab with the filedialogue. I also have tried to pass filename as argument to the file_loader that as well does not work. I still found myself with a FileNotFoundError: [Errno 2] No such file or directory: '' error.

root =Tk()

root.geometry("500x500") 
root.pack_propagate(False) # tells the root to not let the widgets inside it determine its size.
root.resizable(False, False) 

frame = tk.LabelFrame(root, text="CSV/Excel")
frame.place(height=250, width=500)

# Frame for open file dialog
file_frame = tk.LabelFrame(root, text="Opened file")
file_frame.place(height=100, width=400, rely=0.65, relx=0.1)

# Buttons
browse_butt = tk.Button(file_frame, text="Browse A File", command=lambda: file_picker())
browse_butt.place(rely=0.65, relx=0.50)

load_butt = tk.Button(file_frame, text="Load File", command=lambda: file_loader())
load_butt.place(rely=0.65, relx=0.30)

# The file/file path text
label_file = ttk.Label(file_frame, text="Nothing Selected")
label_file.place(x=0, y=0)

 # initialising the treeView
 trv = ttk.Treeview(frame)
 trv.place(relheight=1, relwidth=1)

#============================= Functions under buttons ================================

def file_picker():
    root.filename = filedialog.askopenfilename()
    label_file["text"] = root.filename
    return None

def file_loader():
    Label_path=label_file["text"]

    try:
       csv_file= r''.format(Label_path)
       df= pd.read_csv(csv_file)

    except ValueError:
       messagebox.showerror('File Format Error', 'This program can only read a csv')
    
    return None
      
    clear_data()
    trv['column']=list(df.columns)
    trv['display']="headings"

    for column in trv['column']:
       trv.heading(column, text=column)
    
    df_rows=df.to_numpy().to_list()

    for row in df_rows:
        trv.insert('', 'end', values=row)

    def clear_data():
        trv.delete(*trv.get_children())

    return None
    
martineau
  • 119,623
  • 25
  • 170
  • 301
Camue
  • 469
  • 7
  • 17
  • 2
    `r''.format(Label_path)` is just an overly complicated way of writing the empty string. Why not use `root.filename`, where you actually stored the selected file? – jasonharper Nov 29 '20 at 00:17
  • BTW, you may be interested in the "pandastable" project, which is a tkinter module made specifically to display pandas. – Novel Nov 29 '20 at 00:21
  • The unconditional `return None` you have in the **middle** of the `file_loader()` function prevents anything following that line in it from executing. – martineau Nov 29 '20 at 02:02
  • @Novel when I keep the `return None` in the middle of the file loader and keep follow your code below. I have the my `messagebox` message: `This program can only read csv` but I am selected a csv. there. – Camue Nov 29 '20 at 12:51
  • @martineau Novel when I keep the return None in the middle of the file loader and keep follow your code below. I have the my messagebox message: This program can only read csv but I am selected a csv. there. – Camue Nov 29 '20 at 13:19
  • If you would provide a runnable [mre], then others would probably be able to give you better help. – martineau Nov 29 '20 at 14:09
  • @Novel I have made your the change as advised in the code below. BUT still get the my `messagebox`text – Camue Nov 29 '20 at 15:40

1 Answers1

1

I see what you were trying to do, but the 'r' is only needed for filenames that you directly enter in the source code (aka "hardcoded" or "string literals"). Here you can use the file path directly from the label.

def file_loader():
    try:
       csv_file= label_file["text"]
       df= pd.read_csv(csv_file)

    except ValueError:
       messagebox.showerror('File Format Error', 'This program can only read a csv')
Novel
  • 13,406
  • 2
  • 25
  • 41
  • This is what I get when I try the code above `tkinter.TclError: Invalid column index headings`. – Camue Nov 29 '20 at 12:45
  • That's good, that means the first error is solved. Now you need to work on matching the file you are loading to the display. But that's a different issue. – Novel Nov 29 '20 at 15:16