How can you use Tkinter's filedialog to choose folders and files?
add a comment
To use Tkinter's filedialog to choose folders and files, you can follow these steps:
import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
filedialog.askdirectory()
function:folder_path = filedialog.askdirectory()
filedialog.askopenfilename()
function:file_path = filedialog.askopenfilename()
folder_path = filedialog.askdirectory(initialdir='/',
title='Choose a folder')
file_path = filedialog.askopenfilename(initialdir='/',
title='Choose a file',
filetypes=[('Text Files', '*.txt'),
('All Files', '*.*')])
print(f'Selected folder: {folder_path}')
print(f'Selected file: {file_path}')
root.mainloop()
to start the Tkinter event loop:root.mainloop()
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-01-30 11:00:00 +0000
Seen: 11 times
Last updated: Jul 02 '21