Issue
Using TKinter is there a way to know what file type was chosen in a asksaveasfilename
or a askopenfilename
? I would like to perform a different operation according to the file type that was selected.
Here is my solution, but its has shortcomings:
from pathlib import Path
from tkinter.filedialog import asksaveasfilename
from tkinter.messagebox import showerror
file_path = asksaveasfilename(filetypes=(("Image file", '*.jpg'),
("Text file", '*.txt')))
if file_path:
if Path(file_path).suffix == '.jpg':
print("Image")
elif Path(file_path).suffix == '.txt':
print("Text")
else:
showerror("Save", "Unknown extension '{}'.".format(Path(file_path).suffix))
Solution
Using TKinter is there a way to know what file type was chosen in a asksaveasfilename or a askopenfilename
No, there is not. The dialog has no type information to share. The file types that you specify are merely a filter for the user, which the user is free to use or ignore. The only information you can get from the dialog is whether the user picked a file or not, and the path of the file they picked. There is no other information you can get from the dialog.
Answered By - Bryan Oakley
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.