在文件对话框中设置的 tkinter 文件模式 [英] tkinter file pattern set in a file dialog

查看:30
本文介绍了在文件对话框中设置的 tkinter 文件模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了在文件对话框中获得具有给定扩展名的一组预期文件,我在几个地方看到了写为 ('label','pattern') 的模式,该模式合二为一细绳.但是以下不起作用

To get the set of expected files with given extensions in a file dialog, I've seen in several places written patterns as ('label','pattern'), the pattern being in one string. However the following doesn't work

from tkinter import filedialog as fd
fd.askopenfilenames(
    title='Choose a file',
    filetypes=[('all files', '.*'),
               ('text files', '.txt'),
               ('image files', '.png;.jpg'), # nope,returns *.png;.jpg
               ('image files!', '*.png;*.jpg')]) # neither 

推荐答案

如果您尝试将两个或多个后缀与单个文件类型(例如:图像文件")相关联,有几种方法可以做到.

If you are trying to associate two or more suffixes with a single file type (eg: "image files"), there are a couple of ways to do it.

您可以在单独的行中指定每个后缀.它们将合并为下拉列表中的一项:

You can specify each suffix on a separate line. They will be combined into one item in the dropdown list:

filenames = fd.askopenfilenames(
    title="Choose a file",
    filetypes=[('all files', '.*'),
               ('text files', '.txt'),
               ('image files', '.png'),
               ('image files', '.jpg'),
           ])

使用元组

您也可以将它们指定为元组:

using a tuple

You can also specify them as a tuple:

filenames = fd.askopenfilenames(
    title="Choose a file",
    filetypes=[('all files', '.*'),
               ('text files', '.txt'),
               ('image files', ('.png', '.jpg')),
           ])

这篇关于在文件对话框中设置的 tkinter 文件模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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