On 8/12/14, 1:20 AM, Michael Lange wrote:
Hi,

On Mon, 11 Aug 2014 20:37:38 -0700
JBB <jeanbigbo...@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

Thank you. You are right, askopenfilename is what I want. I wasn't aware that this method existed.

After some more digging, I find that Python uses the macosx backend on initialization. If I change this to tk before beginning work, the problem goes away. My Linux Python uses tk by default.

As a newcomer to Python, I had read that Tkinter was "the" Python GUI tool and started copying code from examples and discussion threads. But, it is clear that there are other tools for GUI programming and that backend selection may have to be matched depending on the package.

JBB



_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss@python.org
https://mail.python.org/mailman/listinfo/tkinter-discuss

Reply via email to