Discussion:
[Tutor] [Tkinter-discuss] tkinter file dialog pattern matching (fwd)
Laura Creighton
2015-08-23 13:52:59 UTC
Permalink
oooh. Seems that there is an undocumented feature we can use!

Laura

------- Forwarded Message

Return-Path: <tkinter-discuss-bounces+lac=***@python.org>
Date: Sun, 23 Aug 2015 12:40:02 +0200
From: Michael Lange <***@web.de>
To: tkinter-***@python.org
Message-Id: <***@web.de>
In-Reply-To: <***@lenny>
References: <***@fido.openend.se>

Hi,

On Sat, 22 Aug 2015 21:04:24 +0100
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
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-***@python.org
https://mail.python.org/mailman/listinfo/tkinter-discuss

------- End of Forwarded Message
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Laura Creighton
2015-08-23 14:00:50 UTC
Permalink
More goodies from Michael Lange

------- Forwarded Message
From: Michael Lange <***@web.de>
To: tkinter-***@python.org

if you only need to hide hidden files, please see my other post. If you
need a more versatile file dialog widget, you might want to consider
wrapping an existing tcl widget for tkinter, this is usually rather easy
once you understand how it is done (all you need is mostly the man page
and a little time) and might save you a lot of headaches.
One example that might be interisting is here:

http://chiselapp.com/user/schelte/repository/fsdialog/index

You might also want to try the Tix.ExFileSelectDialog, it is rather
dated, but actually has a switch to toggle hidden files on/off and
wildcard filtering capabilities. Unfortunately here with debian jessie I
can only test it with Tcl, with python for some reason I get a
segfault :-(

Best regards

Michael
------- End of Forwarded Message
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-08-23 14:39:07 UTC
Permalink
Post by Laura Creighton
You might also want to try the Tix.ExFileSelectDialog, it is rather
dated, but actually has a switch to toggle hidden files on/off and
wildcard filtering capabilities. Unfortunately here with debian jessie I
can only test it with Tcl, with python for some reason I get a
segfault :-(
Aha! Glad it's not just me. That segfault was the obstacle I hit.
I assumed I was just using it wrong but if Michael gets it
too then I don't feel so bad! :-)

But I still think it looks the best option with its dircmd
hook so I'm going to spend some time making it work
(if I can!)
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Laura Creighton
2015-08-23 14:45:11 UTC
Permalink
Post by Alan Gauld
Post by Laura Creighton
You might also want to try the Tix.ExFileSelectDialog, it is rather
dated, but actually has a switch to toggle hidden files on/off and
wildcard filtering capabilities. Unfortunately here with debian jessie I
can only test it with Tcl, with python for some reason I get a
segfault :-(
Aha! Glad it's not just me. That segfault was the obstacle I hit.
I assumed I was just using it wrong but if Michael gets it
too then I don't feel so bad! :-)
But I still think it looks the best option with its dircmd
hook so I'm going to spend some time making it work
(if I can!)
segfaults debian sid, too.

Laura
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Laura Creighton
2015-08-23 15:11:32 UTC
Permalink
But, aha, it works for Python3.5

I wrote this minimal program, to see what needed doing to solve the OP's
problem, and, surprise, it solves it right out of the box.


# -*- coding: utf-8 -*-

from tkinter import *
import tkinter.tix as tix

root = tix.Tk()

def print_selected(args):
print('selected dir:', args)

def pathSelect():
d = tix.ExFileSelectDialog(master=root, command=print_selected)
d.popup()

button = Button(root, text="dialog", command=pathSelect)
button.pack()

root.mainloop()

-------------------
shows my .files or not depending on whether I check the checkbox.

Now , of course I have gotten greedy and want a way to say 'show
me everything but the images' and 'show me everythingbut the .pyc files'

Laura
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-08-23 16:26:04 UTC
Permalink
Post by Laura Creighton
But, aha, it works for Python3.5
I only trued it on 2.7.
I'll have a go on 3.4...
Post by Laura Creighton
I wrote this minimal program, to see what needed doing to solve the OP's
problem, and, surprise, it solves it right out of the box.
from tkinter import *
import tkinter.tix as tix
I believe tix is a complete superset of tkinter so
you only need the tix import.
Post by Laura Creighton
root = tix.Tk()
print('selected dir:', args)
d = tix.ExFileSelectDialog(master=root, command=print_selected)
d.popup()
The dircmd option should work too.
Although you might need to pull out the subwidget first

sub = d.subwidget('fsbox')
sub['dircmd'] = lambda d,f,h: glob.glob(pattern)
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Laura Creighton
2015-08-23 16:20:19 UTC
Permalink
Post by Laura Creighton
segfaults debian sid, too.
Laura
Updating to Python 2.7.10 (default, Jul 1 2015, 10:54:53) and
installing the tix-dev debian package, instead of just tix ... and
I am not sure which of these fixed the problem, because serious I
just did this to help me _find_ what I assumed would still be there ....
made the problem go away.

So this minimal program with Python2 syntax looks like what
Chris Roy-Smith was looking for in the first place.

Laura

# -*- coding: utf-8 -*-

from Tkinter import *
from Tkconstants import *
import Tix
from Tkconstants import *
root = Tix.Tk()

def print_selected(args):
print('selected dir:', args)

def pathSelect():
d =Tix.ExFileSelectDialog(master=root, command=print_selected)
d.popup()

button = Button(root, text="dialog", command=pathSelect)
button.pack()

root.mainloop()


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-08-24 15:39:48 UTC
Permalink
Updating to Python 2.7.10 (default, Jul 1 2015, 10:54:53) and
installing the tix-dev debian
package, ... and I am not sure which of these fixed the problem,
Presumably the 2.7.10 because adding tix-dev does nothing to fix it on
2.7.6...

But 2.7.10 isn't available on Mint 17 yet so i can't confirm positively.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-08-24 16:06:58 UTC
Permalink
Updating to Python 2.7.10 (default, Jul 1 2015, 10:54:53) and
installing the tix-dev debian package,
The seg fault is still there on Python 3.4 and 2.7.6 with tix-dev.
It also happens regardless of the type of dialog I try
(ExFileSelectDialog, FileSelectDialog, DirSelectDialog)

Because its a seg fault rather than a Python error I'm guessing its at
the C level in
either the Python interpreter or in the calling mechanism into the
native tix libraries.
I further assume the native tix code is OK since michael lange got it
working using
native Tcl/tix.

And since its working in both Python 3.5 and 2.7.10 I'm guessing somebody
has fixed it as a known issue or fixed it accidentally in the code base and
the fix has been ported to both the latest builds.

One other option is tix.py itself.
I've looked at the latest tix.py on my system and there are some "FixMe"
lines
so its conceivable that the latest Py releases have an updated tix.py.
Can you check that Laura since I don't have either 3.5 or 2.7.10?
But the FixMe's don't look like they would cause a core dump.

This is beginning to bug me...Although my experience of tix on Python
suggests its not unusual. There are several other useful widgets that I
failed to get working. The documented widgets work fine but the
undocumented ones not so much. :-(
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Laura Creighton
2015-08-24 17:45:50 UTC
Permalink
Maybe a tkinter bug?
https://bugs.python.org/issue11077

There was a mismatch between the tkinter tests and the tkinter shipped
with python 3.4, which I reported here:
https://bugs.python.org/issue24858 -- this is a debian packaging
problem and maybe more things are wrong with it.

***@smartwheels:/usr/lib/python3.5/tkinter$ grep FIXME tix.py

# FIXME: It should inherit -superclass tixShell
# FIXME: It should inherit -superclass tixLabelWidget
# FIXME: It should inherit -superclass tixLabelWidget
# FIXME: It should inherit -superclass tixScrolledHList
# FIXME: It should inherit -superclass tixScrolledHList
# FIXME: It should inherit -superclass tixDialogShell
# FIXME: It should inherit -superclass tixDialogShell
# FIXME: It should inherit -superclass tixStdDialogShell
# FIXME: It should inherit -superclass tixLabelWidget
# FIXME: return python object
# FIXME: This is dangerous to expose to be called on its own.
# FIXME: It should inherit -superclass tixShell
# FIXME: It should inherit -superclass tixScrolledWidget
# FIXME: It should inherit -superclass tixScrolledWidget
# FIXME: It should inherit -superclass tixScrolledWidget
# FIXME: It should inherit -superclass tixScrolledWidget
# FIXME: It should inherit -superclass tixScrolledWidget
# FIXME: It should inherit -superclass tixLabelWidget
# FIXME: It should inherit from Shell
# FIXME: It should inherit -superclass tixScrolledWidget
# FIXME: It should inherit -superclass tixTree
# FIXME: It should inherit -superclass tixScrolledWidget
# FIXME: It should inherit -superclass tixScrolledWidget


Those are the ones I have in 3.5. See if you have more.

Laura
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-08-24 18:02:24 UTC
Permalink
Post by Laura Creighton
# FIXME: It should inherit -superclass tixDialogShell
# FIXME: It should inherit -superclass tixDialogShell
# FIXME: It should inherit -superclass tixStdDialogShell
Nope, these are the same FixMes that I saw.

Its (still) a mystery...
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Laura Creighton
2015-08-24 18:23:02 UTC
Permalink
One other thing -- if you read bugs.python.org about tix issues
you will get the strong idea that as far as python-dev is
concerned, tix is on its way out, and ttk ttk is the way to go,
so if you want your better widget to be part of the new python
distribution, making it a ttk widget seems a good idea.

Laura

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-08-24 20:43:34 UTC
Permalink
Post by Laura Creighton
One other thing -- if you read bugs.python.org about tix issues
you will get the strong idea that as far as python-dev is
concerned, tix is on its way out, and ttk ttk is the way to go,
so if you want your better widget to be part of the new python
distribution, making it a ttk widget seems a good idea.
They are very different beasts. ttk is about native styling.
It doesn't include any new widgets that I'm aware of. Tix by
contrast has the potential tyo offer a very powerful set
of widgets as well as a framework for expansion.

But tix does currently seem a fairly poor offering, PMW
would IMHO have been a better option for an enhanced widget
set. Its certainly more pythonic in nature. Although I'm
not sure if its being maintained whereas Tix is at least
under semi-active development in the Tcl world.

But since I don't even participate much on the main list,
let alone python-dev I can't really comment! :-)
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Chris Roy-Smith
2015-08-24 00:43:19 UTC
Permalink
Post by Laura Creighton
oooh. Seems that there is an undocumented feature we can use!
Laura
------- Forwarded Message
Date: Sun, 23 Aug 2015 12:40:02 +0200
Hi,
On Sat, 22 Aug 2015 21:04:24 +0100
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
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
#############################################
from Tkinter import *
import tkFileDialog as tkfd
root = Tk()
# call a dummy dialog with an impossible option to initialize the file
# dialog without really getting a dialog window; this will throw a
root.tk.call('tk_getOpenFile', '-foobarbaz')
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')
pass
fname = tkfd.askopenfilename()
print(fname)
root.bind('<Control-o>', openfile)
root.mainloop()
#############################################
Best regards
Michael
_______________________________________________
Tkinter-discuss mailing list
https://mail.python.org/mailman/listinfo/tkinter-discuss
------- End of Forwarded Message
_______________________________________________
https://mail.python.org/mailman/listinfo/tutor
Thanks Laura,
That does exactly what I wanted to do.

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Loading...