0

Firstly, PySimpleGUI is amazing! However, I cannot figure out how to show all the files in a folder when using folderbrowse() ?

Alternatively, would it be possible to print the filenames in the selected in an outbox box? Please could I get some guidance on this.

example image when using folderbrowse() Thanks!

  • 1
    what did you try? Show your code in question. Did you get error? Show full error message in question. – furas Sep 03 '20 at 14:52
  • added in image to clarify. I am not getting any errors, I would just like to be able to see the files in the folder, which I am unable to. Would it be possible to print the filenames out? –  Sep 03 '20 at 14:56
  • `folderbrowse()` sould gives you full path to seleced folder and later you should use `os.listdir(folder)` to get names for all files and folders in this folder (but without files in subflders) or `os.walk(folder)` to get for all files and folders in this folder and subloders – furas Sep 03 '20 at 14:57
  • 1
    Do you use `FileBrowse()` or `FolderBrowse()`? They are for different use. `FolderBrowse()` is for selecting only folder so it doesn't display files. `FileBrowse()` is for selecting file so it show files and folders. In title you mention `FileBrowse`, in question you mention `FolderBrowser`. I don't use Windows so I can't say which one you have on image. You should show also your code. – furas Sep 03 '20 at 15:02
  • I use FolderBrowse(). Is there any way I can print the items found in the folder onto the GUI? –  Sep 03 '20 at 15:53

1 Answers1

3

FileBrowse() and FolderBrowse() are different widgets.

FolderBrowse() is for selecting only folder so it doesn't display files.

FileBrowse() is for selecting file so it show files and folders (but you can't select folder to get it).


FileBrowse() gives full path to selected folder and later you should use

  • os.listdir(folder) to get names for all files and folders in selected folder (but without names in subfolders)
  • os.walk(folder) to get for all files and folders in this folder and subfolders.
  • glob.glob(pattern) to get only some names - ie. glob.glob(f"{folder}/*.png")

and when you get names then you can print in console or update text in widget.


This minimal example display filenames in console after clicking Submit

import PySimpleGUI as sg
import os

#help(sg.FolderBrowse)
#help(sg.FileBrowse)

layout = [
    [sg.Input(), sg.FileBrowse('FileBrowse')],
    [sg.Input(), sg.FolderBrowse('FolderBrowse')],
    [sg.Submit(), sg.Cancel()],
]

window = sg.Window('Test', layout)

while True:
    event, values = window.read()
    #print('event:', event)
    #print('values:', values)
    print('FolderBrowse:', values['FolderBrowse'])
    print('FileBrowse:', values['FileBrowse'])
     
    if event is None or event == 'Cancel':
        break
    
    if event == 'Submit':
        # if folder was not selected then use current folder `.`
        foldername = values['FolderBrowse'] or '.' 

        filenames = os.listdir(foldername)

        print('folder:', foldername)
        print('files:', filenames)
        print("\n".join(filenames))
    
window.close()

Similar way you can put text in some widget - ie. MultiLine() - after pressing Submit

import PySimpleGUI as sg
import os

layout = [
    [sg.Input(), sg.FolderBrowse('FolderBrowse')],

    [sg.Submit(), sg.Cancel()],

    [sg.Text('Files')],
    [sg.Multiline(key='files', size=(60,30), autoscroll=True)],

]

window = sg.Window('Test', layout)

while True:
    event, values = window.read()
     
    if event is None or event == 'Cancel':
        break
    
    if event == 'Submit':
        foldername = values['FolderBrowse'] or '.'
        filenames = os.listdir(foldername)
        # it uses `key='files'` to access `Multiline` widget
        window['files'].update("\n".join(filenames))
    
window.close()

BTW: system may give filenames in order of creation so you may have to sort them

 filenames = sorted(os.listdir(foldername))

EDIT:

To get filenames without Submit you may have to use normal Button which will execute code with foldername = PopupGetFolder(..., no_window=True).

import PySimpleGUI as sg
import os

layout = [
    [sg.Input(), sg.Button('FolderBrowse')],

    [sg.Text('Files')],
    [sg.Multiline(key='files', size=(60,30), autoscroll=True)],

    [sg.Exit()],    
]

window = sg.Window('Test', layout)

while True:
    event, values = window.read()
    print(event)
     
    if event is None or event == 'Exit':
        window.close()
        break

    if event == 'FolderBrowse':
        foldername = sg.PopupGetFolder('Select folder', no_window=True)
        if foldername: # `None` when clicked `Cancel` - so I skip it
            filenames = sorted(os.listdir(foldername))
            # it use `key='files'` to `Multiline` widget
            window['files'].update("\n".join(filenames))
        
furas
  • 134,197
  • 12
  • 106
  • 148
  • Thank you SO much for the detailed answer. I really appreciate it! Have a good day! –  Sep 03 '20 at 17:17