2

file_types() is not available on macOS.

While browsing it should only show PNG files.

Does anyone know an alternative way to browse through multiple PNG files?

Line 9

# /usr/bin/python3
# -*- coding: utf-8 -*-

import PySimpleGUI as sg

def main():

    layout = [
             [sg.FilesBrowse(button_text='Galaxie(n) hochladen', file_types=('.png'), key='_FILES_')],
             [sg.OK(),
              sg.Cancel()]
             ]

    window = sg.Window('Vorverarbeitung', layout, background_color='#1e1e1e')

    while True:
        event, values = window.read()
        print(values['_FILES_'].split(';'))
        if event in (None, 'Exit'):
            break
    window.close()

2 Answers2

1
file_types=('.png')    # value in str format

Wrong MacOS format for option file_types, should be in Tuple[(str, str), ...]

file_types=(
    ('All PNG Files', '*.png'), 
    ('All Files', '*.*'),
)

Revised code

# /usr/bin/python3
# -*- coding: utf-8 -*-

import PySimpleGUI as sg

def main():

    layout = [
        [sg.Input(key='_INPUT_'),
         sg.FilesBrowse(button_text='Galaxie(n) hochladen', file_types=(('All PNG Files', '*.png'), ('All Files', '*.*')), key='_FILES_')],
        [sg.OK(),
         sg.Cancel()],
    ]

    window = sg.Window('Vorverarbeitung', layout, background_color='#1e1e1e')

    while True:
        event, values = window.read()
        if event in (None, 'Exit'):
            break
        print(values['_INPUT_'].split(';'))
        print(values['_FILES_'].split(';'))

    window.close()

main()

It looks like this option turned off in PySimpleGUI for running on MacOS with problem.

            if running_mac():
                file_name = tk.filedialog.askopenfilenames(initialdir=self.InitialFolder)
            else:
                file_name = tk.filedialog.askopenfilenames(filetypes=filetypes, initialdir=self.InitialFolder, parent=self.ParentForm.TKroot)
Jason Yang
  • 11,284
  • 2
  • 9
  • 23
  • Thank you @Jaon Yang, but it's still not working :/ –  Aug 31 '21 at 04:46
  • 1
    Maybe there're still something wrong in your new code, update as above. – Jason Yang Aug 31 '21 at 05:07
  • 1
    Still wrong ? what's it ? No any information about it, you may get nothing return. Remember not to tell the failure in your words only, like the wrong title of this issue. – Jason Yang Aug 31 '21 at 06:52
  • 1
    I agree, it doesn't work on Mac (my is Big Sur 11.4, Python 3.9). @Jason, with your code, I can still see all the file types next to each other (.txt, .pdf, .gif, .png, .log, .yaml ...). In my app, it also doesn't work. This seems to be a macOS bug, see also https://stackoverflow.com/questions/58799432/macos-tkinter-how-does-filetypes-of-askopenfilename-work (other than in this example, I don't get any visible Filter at all). – Lenka Čížková Aug 31 '21 at 11:37
0

In the main PySimpleGUI documentation, if you search for file_types you'll find this:

enter image description here

[ EDIT ] One of the reasons the PySimpleGUI project recommends opening issues on the GitHub rather than posting here is that the Issue form contains this checklist, meant to save you considerable amounts of time. Even if you don't want to post an Issue, the next time you've got a question/program, take a look at the GitHub issues checklist. It may save you and others a lot of time.

  • [ ] Searched main docs for your problem www.PySimpleGUI.org
  • [ ] Looked for Demo Programs that are similar to your goal Demos.PySimpleGUI.org
  • [ ] If not tkinter - looked for Demo Programs for specific port
  • [ ] For non tkinter - Looked at readme for your specific port if not PySimpleGUI (Qt, WX, Remi)
  • [ ] Run your program outside of your debugger (from a command line)
  • [ ] Searched through Issues (open and closed) to see if already reported Issues.PySimpleGUI.org
  • [ ] Tried using the PySimpleGUI.py file on GitHub. Your problem may have already been fixed but not released
Mike from PSG
  • 5,312
  • 21
  • 39