1

I want to write a python GUI through Tkinter to read the directory of a csv file. But I noticed that the code I have can only return the folder path instead of the file path. Is there any way I can do to track the csv file path. Here is my code

from Tkinter import *
from tkFileDialog import askdirectory
def browser():
    dir = askdirectory()
    if dir:
        path.set(dir)
mGui = Tk()
path = StringVar()
en = Entry(mGui, textvariable=path)
en.pack()
butt = Button(mGui, text="Browse", command=browser)
butt.pack()
mGui.mainloop()

1 Answer 1

2

Use tkFileDialog.askopenfilename() or tkFileDialog.asksaveasfilename() insead.

from tkFileDialog import askopenfilename

def browser():
    name = askopenfilename()
    if name:
        path.set(name)

....
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that's exactly I want. At the first beginning, I thought askopenfilename() will only return the name of the file, so I just ignored this function. That's a serious mistake I have made. I should try it out first. LOL, really a lesson to learn! Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.