我正在尝试从列表框中添加和删除项目,但出现以下错误:
files = self.fileList()
TypeError: 'list' object is not callable
如果我不能调用它,我如何访问这个列表?我试图将它用作全局变量,但也许我使用不正确。我希望能够从该列表框中获取项目,并在按下按钮时将它们添加到另一个列表框中。
class Actions:
def openfile(self): #select a directory to view files
directory = tkFileDialog.askdirectory(initialdir='.')
self.directoryContents(directory)
def filename(self):
Label (text='Please select a directory').pack(side=TOP,padx=10,pady=10)
files = []
fileListSorted = []
fileList = []
#display the contents of the directory
def directoryContents(self, directory): #displays two listBoxes containing items
scrollbar = Scrollbar() #left scrollbar - display contents in directory
scrollbar.pack(side = LEFT, fill = Y)
scrollbarSorted = Scrollbar() #right scrollbar - display sorted files
scrollbarSorted.pack(side = RIGHT, fill = Y)
#files displayed in the left listBox
global fileList
fileList = Listbox(yscrollcommand = scrollbar.set)
for filename in os.listdir(directory):
fileList.insert(END, filename)
fileList.pack(side =LEFT, fill = BOTH)
scrollbar.config(command = fileList.yview)
global fileListSorted #this is for the filelist in the right window. contains the values the user has selected
fileListSorted = Listbox(yscrollcommand = scrollbarSorted.set) #second listbox (button will send selected files to this window)
fileListSorted.pack(side=RIGHT, fill = BOTH)
scrollbarSorted.config(command = fileListSorted.yview)
selection = fileList.curselection() #select the file
b = Button(text="->", command=lambda:self.moveFile(fileList.curselection()))#send the file to moveFile to be added to fileListSorted
b.pack(pady=5, padx =20)
def moveFile(self,File):
files = self.fileList()
insertValue = int(File[0]) #convert the item to integer
insertName = self.fileList[insertValue] #get the name of the file to be inserted
fileListSorted.insert(END,str(insertName)) #insertthe value to the fileList array
我将文件更改为以下内容以查看文件是否设置正确并返回一个空数组
files = self.fileList
print files
#prints []