Re: tkinter file dialog pattern matching (fwd)
Chris Roy-Smith <[email protected]>
| Newsgroups | gmane.comp.python.tkinter,gmane.comp.python.tutor |
|---|---|
| Message-ID | <[email protected]> |
On 23/08/15 23:52, Laura Creighton wrote: > oooh. Seems that there is an undocumented feature we can use! > > Laura > > ------- Forwarded Message > > Return-Path: <[email protected]> > Date: Sun, 23 Aug 2015 12:40:02 +0200 > From: Michael Lange <[email protected]> > To: [email protected] > Message-Id: <[email protected]> > In-Reply-To: <20150822210424.321b826f@lenny> > References: <[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 > > _______________________________________________ > Tkinter-discuss mailing list > [email protected] > https://mail.python.org/mailman/listinfo/tkinter-discuss > > ------- End of Forwarded Message > _______________________________________________ > Tutor maillist - [email protected] > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > Thanks Laura, That does exactly what I wanted to do.