2015-08-31 252 views
0

我現在有這個tkintr應用程序將允許你上傳文件或多個文件中的Tkinter Python中上傳的文件數量。我希望能夠在控制檯上打印選擇多少個文件。我怎樣才能得到

目前我只能打印什麼選擇的文件與print self.uploadedfilenames

我試圖做len(self.uploadedfilenames)但我得到了一些52一個文件,我不明白這是什麼

#!/usr/bin/env python 
from Tkinter import * 
import tkFileDialog 
import tkMessageBox 



class Application(object): 

    def __init__(self, master): 
     frame = Frame(master) 
     frame.pack() 


     self.file_opt = options = {} 
     options['defaultextension'] = '.txt' 
     options['filetypes'] = [('all files', '.*'), ('text files', '.txt')] 
     options['initialdir'] = 'C:\\' 
     options['initialfile'] = 'myfile.txt' 
     options['parent'] = master 
     options['title'] = 'This is a title' 



     #UPLOAD SECTION 
     Label(frame, text='Upload: ').grid(row=1, column=1) 
     self.upload = Button(frame, text = "Browse", command = self.askopenfile, width = 10).grid(row=1, column=2) 


    def askopenfile(self): 
     self.uploadedfilenames = tkFileDialog.askopenfilenames(multiple=True) 
     if self.uploadedfilenames == '': 
      tkMessageBox.showinfo(message="No file was selected") 
      return 
     else: 
      print len(self.uploadedfilenames) 

root = Tk() 
root.title('application') 
_ = Application(root) 
root.mainloop() 

有沒有辦法找出有多少文件?

+0

對我來說,它完美的作品,你會想到使用'len'。你確定你沒有做一些奇怪的事嗎? – nbro

+0

它給了我一個數字,例如(54,71等)。然而,是不是文件中選擇@Axl –

+0

請數量,展示我們到底使用的是做到這一點的代碼,因爲這不是你正在使用的代碼。 – nbro

回答

1

你看到的長度是因爲它沒有像預期的那樣返回一個元組,而是返回一個字符串。這是由於Windows錯誤。

當我測試我的電腦一切你的代碼是好的。 請看看這裏:

Parsing the results of askopenfilenames()?

此外,當你有這樣的問題,只需打印

self.uploadedfilenames 

然後,你可以看到什麼是返回。那會幫助你解決我的問題。

+0

'打印self.uploadedfilenames'打印文件的路徑選擇 是有辦法解決的Windows錯誤? –

+0

如果函數返回帶有文件名的字符串,就像我鏈接的示例一樣,您只需從字符串中提取文件名即可。這可以使用.split()方法或使用正則表達式或您喜歡的任何東西來完成。 –