Reputation: 1293
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
Reputation: 369354
Use tkFileDialog.askopenfilename()
or tkFileDialog.asksaveasfilename()
insead.
from tkFileDialog import askopenfilename
def browser():
name = askopenfilename()
if name:
path.set(name)
....
Upvotes: 2