Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

open multiple filenames in tkinter and add the filesnames to a list

Tags:

what I want to do is to select multiple files using the tkinter filedialog and then add those items to a list. After that I want to use the list to process each file one by one.

#replace.py import string def main():         #import tkFileDialog         #import re         #ff = tkFileDialog.askopenfilenames()         #filez = re.findall('{(.*?)}', ff)         import Tkinter,tkFileDialog         root = Tkinter.Tk()         filez = tkFileDialog.askopenfilenames(parent=root,title='Choose a file') 

Now, I am able to select multiple files, but I dont know how to add those filenames to the list. any ideas?

like image 676
faraz Avatar asked May 28 '13 10:05

faraz


People also ask

How do I open multiple files in Filedialog Python?

Use the askopenfilenames() function to display an open file dialog that allows users to select multiple files. Use the askopenfile() or askopenfiles() function to display an open file dialog that allows users to select one or multiple files and receive a file or multiple file objects.

How do I use Askopenfilename in Python?

The filename in your sample code is just that -- a string indicating the name of the file you wish to open. You need to pass that to the open() method to return a file handle for the name. You can then read from the file handle. Here's some quick and dirty code to run in the Python interpreter directly.

How do I drag and drop files in tkinter?

The tkinter. dnd module provides drag-and-drop support for objects within a single application, within the same window or between windows. To enable an object to be dragged, you must create an event binding for it that starts the drag-and-drop process.


1 Answers

askopenfilenames returns a string instead of a list, that problem is still open in the issue tracker, and the best solution so far is to use splitlist:

import Tkinter,tkFileDialog  root = Tkinter.Tk() filez = tkFileDialog.askopenfilenames(parent=root, title='Choose a file') print root.tk.splitlist(filez) 

Python 3 update:

tkFileDialog has been renamed, and now askopenfilenames directly returns a tuple:

import tkinter as tk import tkinter.filedialog as fd  root = tk.Tk() filez = fd.askopenfilenames(parent=root, title='Choose a file') 
like image 132
A. Rodas Avatar answered Sep 20 '22 11:09

A. Rodas