python - 我怎样才能用 tkinter 只接受某些文件?

标签 python tkinter

我想接受某个文件,config.cfg , 在 tkinter 中 askopenfilename对话框,这是我的代码。

mod_config_path = tkinter.filedialog.askopenfilename(parent=None, 
                                                     title="Select config.cfg file", 
                                                     initialdir=steamapps, 
                                                     filetypes=[("config.cfg", "config.cfg")])

问题是,filetypes=[("config.cfg", "config.cfg")]没有按照我的意思去做。

我只想接受 config.cfg文件,而不是 misc_config.cfgotherconfig.cfgconfig.cfg 结尾.但如下所示,它接受 *config.cfg文件和 misc_config.cfg文件也显示在图像中。

image

所以,我想做的是,删除 **config.cfg这样它只接受 config.cfg文件。

我在互联网上找不到任何相关的答案,这太令人失望了......

最佳答案

总结我的评论作为答案:

official docs of tcl对于 tk 说:

On the Unix and Macintosh platforms, extensions are matched using glob-style pattern matching. On the Windows platform, extensions are matched by the underlying operating system.

因此,如果您在 Windows 下,要么编写自己的 FileDialog,要么 try catch FileOK Event通过 ctypes 并触发一个虚拟事件。

或者,您可以遵循@CoolCloud 的建议并在对话后验证文件。

关于python - 我怎样才能用 tkinter 只接受某些文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66819122/

相关文章:

python - Tkinter 顶级小部件不显示 - python

python - Canopy 与命令行中运行脚本的区别

python - 在 OS X 上的 virtualenv 中为 scrapy 安装加密时发生错误

python - 如何查看文件的更改?

python - numpy python 快速有效地将非零值提升到二维数组的顶部

python - 运行docker镜像提供-无法找到镜像错误

python - tkinter 标签在一段时间后消失

python - 文本叠加在文本框中

python - 如何将 tkinter 窗口放在其他窗口之上?

python - 什么时候值得在 if-else 语句上使用循环?