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" .
Yes. Somehow filters are put like :
tkFileDialog.askopenfilename(defaultextension='.jpg', filetypes=[('All files','*.*'), ('PNG pictures','*.png'), ('JPEG pictures','*.jpg')]) 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) Source: https://ru.stackoverflow.com/questions/416663/
All Articles