mathgeniuszach / xdialog

A cross-platform wrapper for native dialogs written entirely in python.
MIT License
22 stars 3 forks source link

Open one file or multiple files #3

Closed my1e5 closed 2 years ago

my1e5 commented 2 years ago

I want the user to be able to open one file or multiple files. But it seems that with xdialog.open_file I have to choose between only allowing one file or only allowing multiple files?

If I specify multiple=True for xdialog.open_file then if I only select one file it returns an empty string. And if I specify multiple=False then obviously I can't select multiple files.

Is there a way I can get both sets of functionality at the same time? If one file is selected it returns the one file, if multiple files are selected it returns multiple.

my1e5 commented 2 years ago

I made a modification to open_file in windows_dialogs.py so that I get the functionality I want.


# Return data
    if multiple:
        if ok:
            # Windows splits the parent folder, followed by files, by null characters.
            gen = split_null_list(pfile)
            parent = next(gen)
            files = tuple(parent + "\\" + f for f in gen)
            if files:
                return files
            return parent
        else:
            return ()
    else:
        if ok:
            return file.value
        else:
            return ''

Basically if there are multiple files then files is returned. But if only one file was selected then the parent is returned which is the single file path. Don't know if this is the cleanest implementation, but it was a quick fix I came up with.

I suppose there may be instances where you might want to force the user to select multiple files, but I think it's also nice to have the option of selecting any amount of files. Would be nice if that was implemented. It's a really great module you've made so thank you!

mathgeniuszach commented 2 years ago

Apparently windows only uses the parent if you select one file, huh. But you seem to misunderstand, if multiple=True is set, the result is always supposed to be an iterable, never a string. So if only one file is selected, you will get an iterable containing a single string.

mathgeniuszach commented 2 years ago

Fixed on the pypi in version 1.0.7. Use pip install --upgrade xdialog to update.