Python tkinter askopenfilename 没有响应 [英] Python tkinter askopenfilename not responding

查看:38
本文介绍了Python tkinter askopenfilename 没有响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 tkinter asopenfilename 来触发文件选择器,以便从我的本地目录中读取文件.我的问题是,选择文件后,窗口冻结,python无响应".

I am using tkinter asopenfilename to trigger a file chooser so as to read files from my local directory. My problem is that after the file is chosen, the window freezes and python is 'not responding'.

我已经尝试过这篇文章的答案:Tkinter askopenfilename() 不会关闭 运气不好.

I've tried the answer from this post: Tkinter askopenfilename() won't close no luck.

下面是我的代码:

from tkinter import Tk
from tkinter.filedialog import askopenfilename

root = Tk()
root.withdraw() 
root.update()
filename = askopenfilename() 
print(filename)

有什么我遗漏的吗?如果您需要更清楚,请告诉我.谢谢!

Is there anything that I am missing? Let me know if you need more clarity. Thanks!

推荐答案

我尝试了上述所有解决方案,但似乎没有为我解决同样的问题.对话框正在打开,但在后台的某个地方.

I tried all above solutions but didn't seem to solve the same issue for me. The dialog box was opening but somewhere in the background.

在别处找到了这段代码,它对我来说就像一个魅力.在 Windows 10 上,python 3.x 和使用 Jupyter Notebook.

Found this code elsewhere and it works like a charm for me. On windows 10 too, python 3.x, and using Jupyter Notebook.

把它张贴在这里以防它可以帮助其他人.

Posting it here in case it could help others.

    import tkinter as tk
    from tkinter import filedialog
    root = tk.Tk()
    root.withdraw()
    root.call('wm', 'attributes', '.', '-topmost', True)
    file_path = filedialog.askopenfilename( 
    %gui tk
    print(file_path)

这篇关于Python tkinter askopenfilename 没有响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆