Skip to content Skip to sidebar Skip to footer

Tkinter File Pattern Set In A File Dialog

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. Howev

Solution 1:

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.

declare each suffix separately

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')),
           ])

Solution 2:

Use tuple ('.png', '.jpg')

 ('image files', ('.png', '.jpg')),

Solution 3:

importtkinteroptions= {}
options['defaultextension'] = '.txt'
options['filetypes'] = [('all files', '.*'), ('text files', '.txt'),('asc files', '.asc')]
options['initialdir'] = '.'
file_open = tkinter.filedialog.askopenfile(mode='r', **options)

Post a Comment for "Tkinter File Pattern Set In A File Dialog"