Reputation: 13
i have a simple function that i want to use to open a file dialog.
def open_music():
filename = filedialog.askopenfilename()
it by default opens my documents directory. i want it to allow me to access a music folder with in my D drive.
Upvotes: 0
Views: 3646
Reputation: 811
Yes, you are almost there. Just give the value of initial directory (starting directory) using the initialdir
attribute. Following is how you do it:
# I am just assuming that 'D:\Music' is the path to the directory
filename = filedialog.askdirectory(initialdir='D:\Music')
If you get an error in the directory name above, try the following:
filename = filedialog.askdirectory(initialdir='D://Music')
Hope that helps!
Upvotes: 1
Reputation: 3117
Try This:
from tkinter import *
from tkinter import filedialog
root = Tk()
def open():
filename = filedialog.askopenfilename(initialdir='D:\Music', title="Select Music")
print(filename)
button = Button(root, text="Open Music Folder in D Drive", command=open)
button.pack()
root.mainloop()
Upvotes: 0