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

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


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,tkFileDialogroot = 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 tkimport tkinter.filedialog as fdroot = tk.Tk()filez = fd.askopenfilenames(parent=root, title='Choose a file')


askopenfilenames

returns a tuple of strings, not a string.Simply store the the output of askopenfilenames into filez (as you've done) and pass it to the python's list method to get a list.

filez = tkFileDialog.askopenfilenames(parent=root,title='Choose a file')lst = list(filez)>>> type(lst)<type 'list'>


Putting together parts from above solution along with few lines to error proof the code for tkinter file selection dialog box (as I also described here).

import tkinter as tkfrom tkinter import filedialogroot = tk.Tk()root.withdraw()root.call('wm', 'attributes', '.', '-topmost', True)files = filedialog.askopenfilename(multiple=True) %gui tkvar = root.tk.splitlist(files)filePaths = []for f in var:    filePaths.append(f)filePaths

Returns a list of the paths of the files. Can be stripped to show only the actual file name for further use by using the following code:

fileNames = []for path in filePaths:    name = path[46:].strip()     name2 = name[:-5].strip()     fileNames.append(name2)fileNames

where the integers (46) and (-5) can be altered depending on the file path.