4

Can there a one line for selecting multiple files path at once with PySimpleGUI

sg.PopupGetFile('Unique File select')

Only one file selectable

docs PopupGetFile(message, Message to show in the window
default_path='', Path browsing should start from
default_extension='', Which filetype is the default
save_as=False, Determines which dialog box stype to show
file_types=(("ALL Files", "."),), Which filetypes are displayed
no_window=False, if True no window is displayed except the dialog box
size=(None,None), Size of window
button_color=None, Color of buttons
background_color=None, Color of window background
text_color=None, Color of text in window
icon=DEFAULT_WINDOW_ICON, Icon to show on taskbar
font=None, Font to use
no_titlebar=False, If True does not display a titlebar
grab_anywhere=False, if True can grab window anywhere to move it
keep_on_top=False, if True window will be on top of others
location=(None,None)) Location on screen to show window

Any API for multiple files without looping pattern ?

BadRequest
  • 382
  • 1
  • 14
Sylvain Page
  • 581
  • 1
  • 7
  • 21

1 Answers1

7

Here is a "one-line" solution like you are seeking....

It allows you to select multiple files from a dialog box and will print the list of files as a list. To be safe you should check to make sure the OK button was clicked by looking at the value of event

import PySimpleGUI as sg

event, values = sg.Window('Window Title').Layout([[sg.Input(key='_FILES_'), sg.FilesBrowse()], [sg.OK(), sg.Cancel()]]).Read()

print(values['_FILES_'].split(';'))

[ EDIT May 2022 ]

OK, well, here we are again at SO with the all-too-familiar problem of nothing dies here.... everything stays around, forever, including old problems with old solutions and are no longer a "problem".

While this is an interesting solution, it's from April 2019, only 9 months from the initial launch of PySimpleGUI. This answer is ancient, it works, but it's teaching bad conventions and approaches.

This discussion uses the non-PEP8 popup calls, it's using the Window.Layout method which should not be used, and the key naming conventions recommended by the PySimpleGUI project now don't match.

I didn't know PEP8 existed when naming the original popup calls and it took a while to get aliases for all of them created that do adhere to PEP8 conventions once I figured out that rather grave error.

The PySimpleGUI documentation and demo programs are kept up to date... these answers on StackOverflow are not. It's impossible to find all places on the internet where answers are posted and fix them. But the documentation and sample programs on GitHub are possible to be maintained...and they are.

One "problem" that arises with the strong desire for PySimpleGUI to be backward compatible and thus enable programs written in the first month PySimpleGUI was released is that it doesn't prohibit using calls that are no longer recommended.

The new answer

Ask on GitHub

The way I would answer this question today, in 2022, would be to first say please ask via an Issue on GitHub. Not just for this project, but for all projects, go to the project's home and ask the developers. When looking for answers, go THERE to find the answers. You may find an answer here, but it could come at a cost. Start with the project's documentation and then spread out from there.

"Googling it" is not the best approach to programming. It will lead you to old, possibly bad information. Their algorithm is stupid, you, reader, are not.

The popups

popup_get_file is the PEP8 compliant name of the popup call and should be what's used. It now has a parameter called multiple_files that when set to True will allow your user to select more than 1 file. Check out the call reference tab in the main documentation for details - http://www.PySimpleGUI.org

The description you'll find of that parameter is:

if True, then allows multiple files to be selected that are returned with ';' between each filename

The Final Answer...

files = sg.popup_get_file('Unique File select', multiple_files=True)

This is how you would perform the operation today to get multiple files via a popup call.

Thank you for your attention

It's a long-winded edit, but it's an important topic in a very broad sense. I get wanting to ask the google-genie to get a quick answer, but quick answer isn't what makes solid product or even a good one.

Mike from PSG
  • 5,312
  • 21
  • 39