Re: [Tkinter-discuss] tkinter file dialog pattern matching (fwd)
oooh. Seems that there is an undocumented feature we can use! Laura ------- Forwarded Message Return-Path: <tkinter-discuss-bounces+lac=openend.se@python.org> Date: Sun, 23 Aug 2015 12:40:02 +0200 From: Michael Lange <klappnase@web.de> To: tkinter-discuss@python.org Message-Id: <20150823124002.7391f37e21f9b5cfaa91770f@web.de> In-Reply-To: <20150822210424.321b826f@lenny> References: <201508221103.t7MB3kdx010426@fido.openend.se> Hi, On Sat, 22 Aug 2015 21:04:24 +0100 Pawel Mosakowski <pawel@mosakowski.net> 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 Tkinter-discuss@python.org https://mail.python.org/mailman/listinfo/tkinter-discuss ------- End of Forwarded Message
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: <tkinter-discuss-bounces+lac=openend.se@python.org> Date: Sun, 23 Aug 2015 12:40:02 +0200 From: Michael Lange <klappnase@web.de> To: tkinter-discuss@python.org Message-Id: <20150823124002.7391f37e21f9b5cfaa91770f@web.de> In-Reply-To: <20150822210424.321b826f@lenny> References: <201508221103.t7MB3kdx010426@fido.openend.se>
Hi,
On Sat, 22 Aug 2015 21:04:24 +0100 Pawel Mosakowski <pawel@mosakowski.net> 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 Tkinter-discuss@python.org https://mail.python.org/mailman/listinfo/tkinter-discuss
------- End of Forwarded Message _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Thanks Laura, That does exactly what I wanted to do.
participants (2)
-
Chris Roy-Smith -
Laura Creighton