[Tkinter-discuss] tkFileDialog.askopenfile() mac

Michael Lange klappnase at web.de
Tue Aug 12 10:20:23 CEST 2014


Hi,

On Mon, 11 Aug 2014 20:37:38 -0700
JBB <jeanbigboute at gmail.com> wrote:

> Hello,
> 
> I am attempting to use tkFileDialog.askopenfile():
> 
> 1) On my Mac OSX Mavericks w/ Python 2.7.8, Anaconda 2.0.1 (x86_64) the 
> following code (found via web searches) crashes:
> 
> from Tkinter import *
> import tkFileDialog
> root = Tk()
> root.withdraw()
> file = tkFileDialog.askopenfile(parent=root)
> 
(...)

I don't know about the Mac-specific issues, but nevertheless two things
hit my eye here :)
First, I would not recommend using "file" as a variable name because this
way you override one of Python's built-ins (though I don't think this has
anything to do with the crash, except if you accidentally try to use the
built-in later in your code).
Second, are you sure that you want to use askopenfile () and not
askopenfilename() ? The latter just returns the filename as a string
(which is what's intended in most cases) whereas the first returns a
readily opened file object. Quoting the code from tkFileDialog.py:

    # FIXME: are the following  perhaps a bit too convenient?

    def askopenfile(mode = "r", **options):
        "Ask for a filename to open, and returned the opened file"

        filename = Open(**options).show()
        if filename:
            return open(filename, mode)
        return None

Regards

Michael

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

War is never imperative.
		-- McCoy, "Balance of Terror", stardate 1709.2


More information about the Tkinter-discuss mailing list