0

Using python 3.3 on a unix platform. I have seen many examples where the following code works but I am having an issue. When I select multiple files, I get an error message dialog box: "File /python/input_files/file1.txt file2.txt" does not exist. The error makes sense (tries to open a string of multiple files) but don't understand why others don't see it and how do I correct it. selectFiles is called via a button select. Appreciate any help.

from tkinter import *
from tkinter import ttk
from tkinter.filedialog import askopenfilenames

def selectFiles(self):
    files = askopenfilenames(filetypes=(('Text files', '*.txt'),
                                   ('All files', '*.*')),
                                   title='Select Input File'
                                   )
    fileList = root.tk.splitlist(files)
    print('Files = ', fileList)

Here is the complete code:

#!/usr/local/bin/python3.3

from tkinter import *
from tkinter import ttk
from tkinter.filedialog import askopenfilenames

class multifilesApp(Frame):
    def __init__(self,master=None):
        Frame.__init__(self,master)                                 

    def initializeUI(self):
        self.master.title('Select Files Application')
        self.grid(row=0, column=0,sticky=W)

        # Create the button to select files
        self.button1 = Button(self.master, text='Select Files', command=self.selectFiles, width=10)
        self.button1.grid(row=30, column=0)

    def selectFiles(self):
        files = askopenfilenames(filetypes=(('Text files', '*.txt'),
                                       ('All files', '*.*')),
                                       title='Select Input File'
                                       )
        InputFileList = root.tk.splitlist(files)
        print('Files = ', InputFileList)


# Begin Main
if __name__ == "__main__":
    root = Tk()
    root.minsize(width=250, height=400)
    root.geometry("1200x800")

    # Call the parser GUI application
    app = multifilesApp(master=root)
    app.initializeUI()

    app.mainloop()
furas
  • 134,197
  • 12
  • 106
  • 148
user3748852
  • 1
  • 1
  • 2
  • Always show full error message - there is more information. – furas Jun 18 '14 at 14:53
  • Open files one-by-one in `for` loop. Maybe you will have to add full path to every filename. And check `files` first - someone can close `askopenfilenames` with `cancel` button. – furas Jun 18 '14 at 14:55
  • I tried your code on Linux Mint 16, Python 3.3.2+ and it works. I got separated files with full path `Files = ('/home/furas/furas_date.txt', '/home/furas/tcsh.txt')`. Check your error message again - there is number of line with problem. Problem can be in different place. – furas Jun 18 '14 at 15:02
  • Full error message was provided. There are no line numbers to the problem. I thought of using a loop to read files but want to avoid it, for now. Platform is a sun4u with OS version 5.10 if it matters. Thanks for your response! – user3748852 Jun 19 '14 at 16:08
  • Well, It can be problem with `Tkinter` on Sun4u - or with `Tcl/Tk` (because `Tkinter` is only wrapper on `Tcl/Tk` language). You could try to run some example in Tcl/Tk to test it. See example in my answer. – furas Jun 19 '14 at 18:16

1 Answers1

0

Maybe there is problem with Tcl/Tk on Sun4u

Try to run example in Tcl/Tk (example.tcl)

package require Tk

set filename [tk_getOpenFile -multiple true]

puts $filename

Run (probably):

tclsh example.tcl
furas
  • 134,197
  • 12
  • 106
  • 148