1

I'm trying to open a simple file dialog to ask the user to select a file. The code I have is:

from Tkinter import Tk
from tkFileDialog import askopenfilename

Tk().withdraw()
filename = askopenfilename()
print(filename)
sys.exit(0)

The program retrieves the file name successfully, however the window remains open and does not close. The only way I can exit it is through Force Quit. I am using Mac OS X 10.11 and Python 2.7.11.

Thank you

1 Answers1

0

There seems to be some issues based on your development environment. See reference [2]. This worked for me:

from Tkinter import Tk
from tkFileDialog import askopenfilename

root = Tk() #To initialize Tkinter, we have to first create a Tk root widget
filename = askopenfilename() # store filename
# root.mainloop() may be necessary for your development environment (see [2])
root.destroy() #destroy the event loop - only required in some python environments (see [2])
print(filename)

[1] http://effbot.org/tkinterbook/tkinter-hello-tkinter.htm

[2] http://effbot.org/tkinterbook/tkinter-hello-again.htm

Blane
  • 643
  • 8
  • 13