How can I only accept certain file with tkinter?

Viewed 75

I'd like to accept certain file, config.cfg, in tkinter askopenfilename dialog and here is my code.

mod_config_path = tkinter.filedialog.askopenfilename(parent=None, 
                                                     title="Select config.cfg file", 
                                                     initialdir=steamapps, 
                                                     filetypes=[("config.cfg", "config.cfg")])

The problem is, filetypes=[("config.cfg", "config.cfg")] doesn't do what I meant to do.

I wanted to accept only config.cfg file, not misc_config.cfg or otherconfig.cfg that ends with config.cfg. But as shown below, it accepts *config.cfg file and misc_config.cfg file is also shown in the image.

image

So, what I want to do is, removing * in *config.cfg so that it only accepts config.cfg file.

I couldn't find any related answers around the internet and this is so disappointing...

1 Answers

To summarize my comments as an answer:

The official docs of tcl for tk says:

On the Unix and Macintosh platforms, extensions are matched using glob-style pattern matching. On the Windows platform, extensions are matched by the underlying operating system.

So if you're under windows, either you write your own FileDialog or you try to catch the FileOK Event via ctypes and trigger a virtual event.

Alternatively you can follow @CoolCloud advice and verify file after the dialog.

Related