Max Koning
Max Koning

Reputation: 172

Tkinter dialog to open file or directory

I have a tool where it is possible to open a dialog with a button. There is a button for a file dialog and a button for directory dialog. I was wondering if there is something in Tkinter where I can have one button that opens a dialog that will except a file or a directory. So I want to go from two different dialogs to just one. Is there something like this in Tkinter or is it possible to create this? Maybe something like an extension filter but then you can select whether you want to choose a file or directory.

Thanks in advance!

Upvotes: 1

Views: 513

Answers (1)

Max Koning
Max Koning

Reputation: 172

I have solved it in a different way. The only downside is that I can't select multiple files or folders, but other than that it does exactly what I wanted it to do.

Here is the code I used:

mydocs_pidl = shell.SHGetFolderLocation(0, shellcon.CSIDL_DRIVES, 0, 0)
    pidl, display_name, image_list = shell.SHBrowseForFolder(
        win32gui.GetDesktopWindow(),
        mydocs_pidl,
        "Select a file or folder",
        shellcon.BIF_BROWSEINCLUDEFILES,
        None,
        None
    )

if (pidl, display_name, image_list) == (None, None, None):
    print("No file or directory selected!")
else:
    path = str(shell.SHGetPathFromIDList(pidl))
    print(path)

Upvotes: 1

Related Questions