我想接受某个文件,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.cfg
或 otherconfig.cfg
以 config.cfg
结尾.但如下所示,它接受 *config.cfg
文件和 misc_config.cfg
文件也显示在图像中。
所以,我想做的是,删除 *
在 *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/