fyr91
fyr91

Reputation: 1293

Python Tkinter return csv file directory

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()

Upvotes: 1

Views: 1369

Answers (1)

falsetru
falsetru

Reputation: 369354

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

from tkFileDialog import askopenfilename

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

....

Upvotes: 2

Related Questions