Re: tkinter file dialog pattern matching

Michael Lange <[email protected]>
Newsgroups gmane.comp.python.tkinter
Message-ID <[email protected]>
Hi,

On Sat, 22 Aug 2015 21:04:24 +0100
Pawel Mosakowski <[email protected]> wrote:

> Hi,
> 
> I've found this little gem in the Tk docs
> https://www.tcl.tk/man/tcl8.4/TkCmd/getOpenFile.htm#M13
> From what I see "file patterns" in the file dialog are not "regex
> patterns" and do not support special characters. Only things that work
> are:
> 1) * - any extension
> 2) "" - files without extension
> 3) literal extension without wildcard chars
> Unfortunately it looks like there is no simple way to filter out hidden
> files.

actually the unix tk file dialog has an an (however undocumented) feature
to hide hidden elements and display even a button that allows to toggle
between hidden elements on/off, however we need to do a little tcl to get
to this. Since the feature is not documented anywhere it might also be a
good idea to wrap this into a try...except. See this little code snippet:

#############################################
from Tkinter import *
import tkFileDialog as tkfd

root = Tk()

try:
    # call a dummy dialog with an impossible option to initialize the file
    # dialog without really getting a dialog window; this will throw a
    # TclError, so we need a try...except :
    try:
        root.tk.call('tk_getOpenFile', '-foobarbaz')
    except TclError:
        pass
    # now set the magic variables accordingly
    root.tk.call('set', '::tk::dialog::file::showHiddenBtn', '1')
    root.tk.call('set', '::tk::dialog::file::showHiddenVar', '0')
except:
    pass

# a simple callback for testing:
def openfile(event):
    fname = tkfd.askopenfilename()
    print(fname)
root.bind('<Control-o>', openfile)

root.mainloop()
#############################################

Best regards

Michael

.-.. .. ...- .   .-.. --- -. --.   .- -. -..   .--. .-. --- ... .--. . .-.

This cultural mystique surrounding the biological function -- you
realize humans are overly preoccupied with the subject.
		-- Kelinda the Kelvan, "By Any Other Name", stardate
4658.9
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.