Tkinter open file window, file extension case sensitivity
Question:
I’m playing a bit with tkinter for one of my scripts and I have trouble using the filetypes
argument for the askopenfilename()
method.
INFILE = askopenfilename(filetypes = (("TEST files", "*.test"), ("all files", "*.*")))
This is working pretty good but the filter is case sensitive, is there any way to make it not ?
I’d like to be able to see all files with .test
extension, no matter what the case is (aka: .teSt .TEST .test
)
I’m pretty sure I don’t have to hard write every single combination, so if you have any idea of how to do this
Answers:
There is no built-in option to do that, but you can for example save the case sensitive extensions in a list and then refer to it:
from tkinter import filedialog
from tkinter import *
text_file_extensions = ['*.txt', '*.txT', '*.tXT', '*.Txt', '*.TXt', '*.TXT', '*.tXt',
'*.TxT']
ftypes = [
('test files', text_file_extensions),
('All files', '*'),
]
root = Tk()
root.filename = filedialog.askopenfilename(initialdir="/", title="Select file",
filetypes=ftypes)
print(root.filename)
Demo:
I’m playing a bit with tkinter for one of my scripts and I have trouble using the filetypes
argument for the askopenfilename()
method.
INFILE = askopenfilename(filetypes = (("TEST files", "*.test"), ("all files", "*.*")))
This is working pretty good but the filter is case sensitive, is there any way to make it not ?
I’d like to be able to see all files with .test
extension, no matter what the case is (aka: .teSt .TEST .test
)
I’m pretty sure I don’t have to hard write every single combination, so if you have any idea of how to do this
There is no built-in option to do that, but you can for example save the case sensitive extensions in a list and then refer to it:
from tkinter import filedialog
from tkinter import *
text_file_extensions = ['*.txt', '*.txT', '*.tXT', '*.Txt', '*.TXt', '*.TXT', '*.tXt',
'*.TxT']
ftypes = [
('test files', text_file_extensions),
('All files', '*'),
]
root = Tk()
root.filename = filedialog.askopenfilename(initialdir="/", title="Select file",
filetypes=ftypes)
print(root.filename)
Demo: