0

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.

0

2 Answers 2

1

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!

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

Comments

0

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

2 Comments

Thanks, that did the job.
@HamzaBodla Glad to help. BTW you can make the answer accepted so it will help others to find the right answer.

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.