Tuesday, January 5, 2016

python tkinter tkFiledialog filetype order

For file dialogs, Tkinter very nicely allows control of the specification of file types to be shown in the dialog. This seems to actually be a native feature of the operating system that Tkinter simply has access to. To write a dialog that restricts the files that the user can chose from, you can set the file type to *.txt for instance, or a list such as [('text file','*.txt'),('shell scripts','*.csh')]. My problem is that I also want to allow the option to the user to select having access to all file types, like this: [('text file','*.txt'),('all files','*.*')], which works exactly as I want EXCEPT THAT which of these two options the dialog comes up restricting to seems completely random. It does not seem to be determinate by order of the list, or even by operating system (the problem occurs in both Windows and Linux at least in my experience). There's a "default file type" option that can be defined, but despite what seems like an obvious intention this also does not effect which of the file types out of my list is used as the default. So, I don't have an answer to this yet, but here are some results from google searches on the topic:

Here is somebody with my exact problem. Sadly nobody answered his question. I have to disagree that the issue is OS related, since the filetype dropbox default comes up differently at different times on my one single Windows system.

http://stackoverflow.com/questions/23392531/how-to-control-the-order-of-file-types-in-tkfiledialog-for-different-windows-pla


The basic man page and options for tkFileDialog:

http://tkinter.unpythonic.net/wiki/tkFileDialog


This answer didn't seem helpful to me since I've tried changing the order of my list also, but maybe it has something to do with how they're explicitly defining a dictionary for their file options? I don't know.

http://stackoverflow.com/questions/11352278/default-file-type-in-tkfiledialogs-askopenfilename-method


The original question in this thread was more simple than mine, but the discussion does show a little bit about how the definition of the file type options is a list of tuples:

https://www.reddit.com/r/learnpython/comments/3loul5/only_showing_certain_filetypes_in_filedialog/


Probably if I read up on the OS DLL for the file dialog itself, it might show how to define the order of the file type list in a way that the OS can understand, and then figure out how to do the same thing in Python. Maybe the key is to use an ordered dictionary instead of a regular dictionary?