File Explorer in Python using Tkinter

File Explorer in Python using Tkinter

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:

1. Prerequisites:

Start by importing the necessary modules:

import tkinter as tk from tkinter import filedialog 

2. Function to Open File Dialog:

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) 

3. Setting Up the GUI:

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() 

Optional Enhancements:

  • Filtering File Types: You can restrict the file types the user can select by using the filetypes argument:
file_path = filedialog.askopenfilename(filetypes=[("Text files", "*.txt"), ("All files", "*.*")]) 
  • Open Multiple Files: To allow the selection of multiple 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) 
  • Choosing Directories: Instead of files, if you want the user to pick a directory:
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.

Examples

  1. Python Tkinter file dialog example:

    • Description: Use the filedialog module in Tkinter to create a simple file dialog for opening or saving files.
    • Code Example:
      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() 
  2. Creating a file explorer in Tkinter:

    • Description: Develop a basic file explorer in Tkinter to navigate through directories and display file information.
    • Code Example:
      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() 
  3. Python Tkinter browse file button:

    • Description: Create a button in Tkinter that triggers the file dialog to select and display a file.
    • Code Example:
      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() 
  4. File handling in Tkinter Python:

    • Description: Perform basic file handling operations in Tkinter, such as reading and displaying the content of a text file.
    • Code Example:
      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() 
  5. Tkinter open file dialog example:

    • Description: Use the askopenfilename method in Tkinter to create an open file dialog and retrieve the selected file path.
    • Code Example:
      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() 
  6. Custom file explorer in Tkinter:

    • Description: Develop a custom file explorer in Tkinter with features like opening, deleting, and copying files.
    • Code Example:
      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() 
  7. Tkinter file and directory selection:

    • Description: Implement a file and directory selection mechanism in Tkinter using filedialog.askopenfilename and filedialog.askdirectory.
    • Code Example:
      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() 

More Tags

userscripts intervention cx-freeze client-certificates fasterxml firebase-realtime-database android-sqlite android-progressbar raspberry-pi http-status-codes

More Programming Guides

Other Guides

More Programming Examples