Add multiple extensions in one filetypes mac - tkinter/filedialog/askopenfilename
It seems that you must separate the wildcard patterns with a space rather than ;
(verified on Python 3.5.1):
from tkinter import filedialog
# add `, initialdir="..."` to set the initial directory shown in the dialog
filedialog.askopenfilename(filetypes=[("Excel files", ".xlsx .xls")])
Note:
- While
"*.xlsx *.xls"
would work too (at least on Windows and macOS), the initial*
isn't necessary.[1] - Whether
.*
or*.*
works (no filter) seems to be platform-dependent; omittingfiletypes
is the better option in that case. - Similarly, the behavior of matching part of the base filename - something like
foo.txt
orfoo*.txt
- appears to be platform-dependent: on Windows, these do work, with a prepended*
(*foo.txt
and*foo*.txt
), whereas on macOS they seem to match nothing.
If the platform-dependent behaviors are instead / also linked to different tkinter versions, do let us know.
[1] Jakub Bláha reports that "*.xlsx *.xls"
didn't actually work for him in Python 3.7.4 on Windows 10 version 1903 (though I don't see the same problem); to be safe, omit the *
if not needed.
At least on Windows, if you want to be able to open multiple filetypes but always only one at a time, your code has to look like this:
filedialog.askopenfilename(filetypes=[("Excel file","*.xlsx"),("Excel file 97-2003","*.xls")])
If you want to be able to select both file types at the same time without switching between them, they need to have the same name.
filedialog.askopenfilename(filetypes=[("Excel file","*.xlsx"),("Excel file", "*.xls")])
Multiple extensions on Windows 10 work for me like this. For example searching for python files.
py_exts = r"*.py *.py3 *.pyc *.pyo *.pyw *.pyx *.pyd *.pxd *.pyi *.pyi *.pyz *.pywz *.rpy *.pyde *.pyp *.pyt *.xpy *.ipynb"
filename = filedialog.askopenfilename(initialdir=SEEN,
title="Select a File",
filetypes=(("video files",
video_exts),
("python files", py_exts),
("all files",
"*.*")))