[Tkinter-discuss] tkinter file dialog pattern matching (fwd)
Laura Creighton
lac at openend.se
Sun Aug 23 15:52:59 CEST 2015
oooh. Seems that there is an undocumented feature we can use!
Laura
------- Forwarded Message
Return-Path: <tkinter-discuss-bounces+lac=openend.se at python.org>
Date: Sun, 23 Aug 2015 12:40:02 +0200
From: Michael Lange <klappnase at web.de>
To: tkinter-discuss at python.org
Message-Id: <20150823124002.7391f37e21f9b5cfaa91770f at web.de>
In-Reply-To: <20150822210424.321b826f at lenny>
References: <201508221103.t7MB3kdx010426 at fido.openend.se>
Hi,
On Sat, 22 Aug 2015 21:04:24 +0100
Pawel Mosakowski <pawel at 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 at python.org
https://mail.python.org/mailman/listinfo/tkinter-discuss
------- End of Forwarded Message
More information about the Tkinter-discuss
mailing list