Is it possible, by calling askopenfilename , to hide some files?

For example, in the folder there are files with the extension ".jpg" and ".png" , but the user should see only ".png" .

    2 answers 2

    Yes. Somehow filters are put like :

    tkFileDialog.askopenfilename(defaultextension='.jpg', filetypes=[('All files','*.*'), ('PNG pictures','*.png'), ('JPEG pictures','*.jpg')]) 
    • Thanks for the advice, fixed it. Just a long time ago I used Python for the last time and wrote very little on it. - igumnov

    You can use the filetypes parameter to show only favorite files as mentioned @igumnov .

    Here is a self-sufficient example for Python 2 & 3, which prints the name of the file selected by the user, and initially only png-files are shown:

     #!/usr/bin/env python import os try: from Tkinter import Tk from tkFileDialog import askopenfilename except ImportError: # Python 3 from tkinter import Tk from tkinter.filedialog import askopenfilename root = Tk() root.withdraw() # hide the window filename = askopenfilename( parent=root, title='Images', initialdir=os.path.expanduser(u'~/Pictures'), filetypes=[('PNG images', '.png'), ('JPEG images', '.jpg')]) root.destroy() print(filename)