Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Button to import CSV, then return file path as string - but not showing as defined after click

So I am working on my first project, it is a program my team at work can use to run their reports. So the idea is they import a csv, the program pulls the correct data and calculates the result which is displayed.

So I have got to the step where I am able to click the button to browse for the CSV, I then need to pull the file path as a string so when I click the run button it then completes all the calculations.

But I am trying to just call the file path with a print statement to check it works but it keeps telling me filepath is not defined? I have been trying to solve this on my own for almost a full day now. I have tried multiple functions, global/return, if statements, the lot. I can only get the data show once the button is clicked but what I need it to do is store it so I can recall it at the next event (button Run clicked)

Here is my code, I haven't done the Run button function yet, trying to sort this out first. Thanks in advance!

from tkinter import *
import pandas as pd
from tkinter import filedialog
from PIL import ImageTk,Image
import os
import sys
import csv

root = Tk()
root.title("Virtual1 Reporting")
root.iconbitmap("c:/users/Danie/PycharmProjects/V1Reporting/Capture.ico")
root.minsize(600,400)

# Functions
def importing():
    global filepath
    filename = filedialog.askopenfilename(filetypes=((".csv", "*.csv"),))
    file_path_view = Label(root, text=filename).pack()
    filepath = str(filename)
    file = open(filepath)
    reader = csv.reader(file)
    df = list(reader)
    #for x in list(range(9, len(df))):
        #print(df[x][2])

# Objects
Title = Label(root, text="Provisioning Reporting Tool", font=("Courier", 15, "bold")).pack(side=TOP, expand=YES)
v1_logo = ImageTk.PhotoImage(Image.open("C:/Users/Danie/PycharmProjects/V1Reporting/v1logo.png"))
logo = Label(image=v1_logo).pack()
import_button = Button(root, text="Import CSV", padx=20, command=importing)
import_button.pack(side=TOP, expand=YES)
run_button = Button(root, text="Run", padx=30, fg='white', bg='blue').pack(side=TOP, expand=YES)


print(filepath)

Here is my Error:

Traceback (most recent call last):
  File "C:/Users/Danie/PycharmProjects/V1Reporting/venv/XLGui.py", line 35, in <module>
    print(filepath)
NameError: name 'filepath' is not defined

Process finished with exit code 1

1 Answer

It looks like you are not running the importing function anywhere and that is where the filepath gets defined so it is undefined when you try to print it?