Introduction
Basic Widgets
Toplevel Widgets
Geometry Management
Binding Functions
Working with Images in Tkinter
Tkinter Advance
Applications and Projects
Creating a File Explorer using Tkinter is a common task, especially when you want your user to select a file for further processing. The filedialog
module of Tkinter provides a set of dialog windows to cater to this need.
Let's create a simple file explorer that lets the user pick a file and displays the file path:
Start by importing the necessary modules:
import tkinter as tk from tkinter import filedialog
This function utilizes filedialog.askopenfilename()
to open the file explorer and get the file path:
def open_file(): file_path = filedialog.askopenfilename() if file_path: path_label.config(text=file_path)
You'll set up a basic Tkinter window with a button and a label:
root = tk.Tk() root.title("Simple File Explorer") root.geometry("600x100") # Button to open the file explorer open_btn = tk.Button(root, text="Open File", command=open_file) open_btn.pack(pady=20) # Label to display the chosen file path path_label = tk.Label(root, text="No file chosen", wraplength=550) path_label.pack(pady=10) root.mainloop()
filetypes
argument:file_path = filedialog.askopenfilename(filetypes=[("Text files", "*.txt"), ("All files", "*.*")])
def open_files(): files = filedialog.askopenfilenames() if files: file_list = "\n".join(files) path_label.config(text=file_list) open_btn = tk.Button(root, text="Open Files", command=open_files)
def open_directory(): directory = filedialog.askdirectory() if directory: path_label.config(text=directory) open_btn = tk.Button(root, text="Open Directory", command=open_directory)
Using these enhancements, you can create a more feature-rich file explorer to meet specific requirements.
That's it! You've now created a basic file explorer using Tkinter.
Python Tkinter file dialog example:
filedialog
module in Tkinter to create a simple file dialog for opening or saving files.import tkinter as tk from tkinter import filedialog def open_file_dialog(): file_path = filedialog.askopenfilename(title="Open File", filetypes=[("Text Files", "*.txt")]) print(f"Selected file: {file_path}") def save_file_dialog(): file_path = filedialog.asksaveasfilename(title="Save File", defaultextension=".txt", filetypes=[("Text Files", "*.txt")]) print(f"Saved file: {file_path}") root = tk.Tk() root.title("File Dialog Example in Tkinter") open_button = tk.Button(root, text="Open File Dialog", command=open_file_dialog) open_button.pack(pady=10) save_button = tk.Button(root, text="Save File Dialog", command=save_file_dialog) save_button.pack(pady=10) root.mainloop()
Creating a file explorer in Tkinter:
import tkinter as tk from tkinter import filedialog import os def open_file_explorer(): folder_path = filedialog.askdirectory(title="Open Folder") list_files(folder_path) def list_files(folder_path): file_list.delete(1.0, tk.END) # Clear previous content for filename in os.listdir(folder_path): file_list.insert(tk.END, filename + "\n") root = tk.Tk() root.title("File Explorer in Tkinter") open_button = tk.Button(root, text="Open File Explorer", command=open_file_explorer) open_button.pack(pady=10) file_list = tk.Text(root, height=10, width=40) file_list.pack(pady=10) root.mainloop()
Python Tkinter browse file button:
import tkinter as tk from tkinter import filedialog def browse_file(): file_path = filedialog.askopenfilename(title="Select File", filetypes=[("Text Files", "*.txt")]) selected_file_label.config(text=f"Selected File: {file_path}") root = tk.Tk() root.title("Browse File Button in Tkinter") browse_button = tk.Button(root, text="Browse File", command=browse_file) browse_button.pack(pady=10) selected_file_label = tk.Label(root, text="Selected File: None") selected_file_label.pack(pady=10) root.mainloop()
File handling in Tkinter Python:
import tkinter as tk from tkinter import filedialog def open_and_display_file(): file_path = filedialog.askopenfilename(title="Open File", filetypes=[("Text Files", "*.txt")]) with open(file_path, 'r') as file: file_content = file.read() text_widget.delete(1.0, tk.END) # Clear previous content text_widget.insert(tk.END, file_content) root = tk.Tk() root.title("File Handling in Tkinter") open_button = tk.Button(root, text="Open and Display File", command=open_and_display_file) open_button.pack(pady=10) text_widget = tk.Text(root, height=10, width=40) text_widget.pack(pady=10) root.mainloop()
Tkinter open file dialog example:
askopenfilename
method in Tkinter to create an open file dialog and retrieve the selected file path.import tkinter as tk from tkinter import filedialog def open_file_dialog(): file_path = filedialog.askopenfilename(title="Open File", filetypes=[("Text Files", "*.txt")]) print(f"Selected file: {file_path}") root = tk.Tk() root.title("Open File Dialog Example in Tkinter") open_button = tk.Button(root, text="Open File Dialog", command=open_file_dialog) open_button.pack(pady=10) root.mainloop()
Custom file explorer in Tkinter:
import tkinter as tk from tkinter import filedialog import os from shutil import copyfile def open_file_explorer(): folder_path = filedialog.askdirectory(title="Open Folder") list_files(folder_path) def list_files(folder_path): file_list.delete(1.0, tk.END) # Clear previous content for filename in os.listdir(folder_path): file_list.insert(tk.END, filename + "\n") def copy_selected_file(): selected_file = file_list.get(tk.SEL_FIRST, tk.SEL_LAST).strip() source_path = os.path.join(folder_path, selected_file) destination_path = filedialog.asksaveasfilename(title="Save As", initialfile=selected_file) copyfile(source_path, destination_path) list_files(folder_path) root = tk.Tk() root.title("Custom File Explorer in Tkinter") open_button = tk.Button(root, text="Open File Explorer", command=open_file_explorer) open_button.pack(pady=10) file_list = tk.Text(root, height=10, width=40) file_list.pack(pady=10) copy_button = tk.Button(root, text="Copy Selected File", command=copy_selected_file) copy_button.pack(pady=10) folder_path = "" root.mainloop()
Tkinter file and directory selection:
filedialog.askopenfilename
and filedialog.askdirectory
.import tkinter as tk from tkinter import filedialog def select_file(): file_path = filedialog.askopenfilename(title="Select File", filetypes=[("Text Files", "*.txt")]) selected_file_label.config(text=f"Selected File: {file_path}") def select_directory(): directory_path = filedialog.askdirectory(title="Select Directory") selected_directory_label.config(text=f"Selected Directory: {directory_path}") root = tk.Tk() root.title("File and Directory Selection in Tkinter") select_file_button = tk.Button(root, text="Select File", command=select_file) select_file_button.pack(pady=10) selected_file_label = tk.Label(root, text="Selected File: None") selected_file_label.pack(pady=10) select_directory_button = tk.Button(root, text="Select Directory", command=select_directory) select_directory_button.pack(pady=10) selected_directory_label = tk.Label(root, text="Selected Directory: None") selected_directory_label.pack(pady=10) root.mainloop()