Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tkinter open file window, file extension case sensitivity

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

like image 543
Plopp Avatar asked Oct 29 '25 09:10

Plopp


1 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:

enter image description here

like image 186
Billal Begueradj Avatar answered Oct 31 '25 23:10

Billal Begueradj