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?
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.
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.
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.
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With