3

I'm trying to simply get a file name from the user by tkinter.filedialog.askopenfilename(). The function returns fine and the code below displays the file name okay but the dialog window doesn't close immediately after hitting 'open' or 'cancel', it freezes. I'm using python 3.3.3 or OSX 10.9.1 and tcl/tK 8.5.9.

from tkinter import *
from tkinter.messagebox import *
from tkinter.filedialog import *

top = Tk()
top.withdraw()

file_name = filedialog.askopenfilename()

print (file_name)
Drewness
  • 4,879
  • 4
  • 31
  • 49
John Bell
  • 31
  • 1
  • 3
  • Have you tried opening the dialog _after_ starting the event loop? OSX has its share of tkinter idiosyncrasies, but generally speaking tkinter needs an event loop to be running in order to function properly. – Bryan Oakley Feb 27 '14 at 01:11
  • Bryan, by 'an event loop' do you mean mainloop()? – John Bell Feb 27 '14 at 17:53

2 Answers2

0

Adding root.update() after filedialog.askopenfilename() gets the open file dialog to close after the file is chosen.

root = tk.Tk()                     
root.withdraw()
file_path = filedialog.askopenfilename()
root.update()

Refer to: Tkinter askopenfilename() won't close

SparkAndShine
  • 15,667
  • 17
  • 84
  • 129
-2

You don't need to specify module name in

file_name = filedialog.askopenfilename()

Try

file_name = askopenfilename()

instead