Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to open multiple filenames in Tkinter and add the file names to a list?
To open multiple files in a Tkinter application and add their names to a list, we use the tkinter.filedialog package. This creates a dialog box to interact with external files on the system.
Importing the Required Module
First, import the filedialog module ?
import tkinter.filedialog as fd
Using askopenfilenames() Function
The askopenfilenames() function opens a file explorer window that allows users to select multiple files. It returns a tuple of selected file paths.
Syntax
fd.askopenfilenames(parent=None, title="Open files", **options)
Complete Example
Here's a complete example that opens multiple files and displays their names in a list ?
# Import the required libraries
from tkinter import *
from tkinter import ttk
import tkinter.filedialog as fd
# Create an instance of tkinter frame or window
win = Tk()
# Set the geometry of tkinter frame
win.geometry("700x350")
def open_file():
# Open file dialog for multiple file selection
files = fd.askopenfilenames(parent=win, title='Choose Files')
# Convert tuple to list and print
file_list = list(files)
print("Selected files:")
for file in file_list:
print(file)
# Add a Label widget
label = Label(win, text="Select the Button to Open Multiple Files", font=('Arial', 11))
label.pack(pady=30)
# Add a Button Widget
ttk.Button(win, text="Select Files", command=open_file).pack()
win.mainloop()
How It Works
When you run this code and click "Select Files", a dialog box appears allowing you to:
- Select multiple files by holding Ctrl (Windows/Linux) or Cmd (Mac)
- The selected file paths are returned as a tuple
- Convert the tuple to a list for easier manipulation
- Print each file path in the console
Enhanced Example with Display
Here's an improved version that displays selected files in the GUI ?
from tkinter import *
from tkinter import ttk
import tkinter.filedialog as fd
win = Tk()
win.geometry("700x400")
win.title("Multiple File Selector")
# Variable to store file list
selected_files = []
def open_files():
global selected_files
files = fd.askopenfilenames(
parent=win,
title='Choose Multiple Files',
filetypes=[("All files", "*.*"), ("Text files", "*.txt"), ("Python files", "*.py")]
)
# Convert to list
selected_files = list(files)
# Update display
update_display()
def update_display():
# Clear previous content
file_listbox.delete(0, END)
# Add each file to listbox
for file in selected_files:
# Show only filename, not full path
filename = file.split('/')[-1] # For Unix-like systems
if '\' in file: # For Windows
filename = file.split('\')[-1]
file_listbox.insert(END, filename)
count_label.config(text=f"Files selected: {len(selected_files)}")
# Create widgets
label = Label(win, text="Multiple File Selector", font=('Arial', 14, 'bold'))
label.pack(pady=10)
ttk.Button(win, text="Select Files", command=open_files).pack(pady=10)
count_label = Label(win, text="Files selected: 0", font=('Arial', 10))
count_label.pack(pady=5)
# Listbox to display selected files
file_listbox = Listbox(win, height=15, width=80)
file_listbox.pack(pady=10, padx=20, fill=BOTH, expand=True)
win.mainloop()
Key Features
| Feature | Description |
|---|---|
askopenfilenames() |
Opens dialog for multiple file selection |
filetypes parameter |
Filter files by extension |
| Tuple to List conversion | Makes file paths easier to manipulate |
| GUI display | Shows selected files in the interface |
Conclusion
Use askopenfilenames() to select multiple files in Tkinter. Convert the returned tuple to a list for easier manipulation. Add file type filters and GUI display for better user experience.
