Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 调用tkinter askopenfilename方法后,“打开文件”对话框冻结_Python_Macos_Python 3.x_Tkinter - Fatal编程技术网

Python 调用tkinter askopenfilename方法后,“打开文件”对话框冻结

Python 调用tkinter askopenfilename方法后,“打开文件”对话框冻结,python,macos,python-3.x,tkinter,Python,Macos,Python 3.x,Tkinter,我试图通过tkinter.filedialog.askopenfilename()从用户那里获取文件名。函数返回正常,下面的代码显示文件名正常,但在点击“打开”或“取消”后对话框窗口不会立即关闭,它会冻结。我正在使用python 3.3.3或OSX 10.9.1和tcl/tK 8.5.9 from tkinter import * from tkinter.messagebox import * from tkinter.filedialog import * top = Tk() top.w

我试图通过
tkinter.filedialog.askopenfilename()
从用户那里获取文件名。函数返回正常,下面的代码显示文件名正常,但在点击“打开”或“取消”后对话框窗口不会立即关闭,它会冻结。我正在使用python 3.3.3或OSX 10.9.1和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)

您不需要在中指定模块名称

file_name = filedialog.askopenfilename()
试一试

相反,在
filedialog之后添加
root.update()
。askopenfilename()
将在选择文件后关闭打开的文件对话框

root=tk.tk()
root.draw()
file_path=filedialog.askopenfilename()
root.update()

请参阅:

谢谢。刚试过,但描述的行为没有改变。我试过你的代码,效果很好。问题似乎不在代码本身。启动事件循环后是否尝试打开对话框?OSX拥有tkinter特有的特性,但一般来说tkinter需要运行一个事件循环才能正常运行。Bryan,“事件循环”是指mainloop()吗?
file_name = askopenfilename()