Reputation: 2167
I'm trying to add and remove items from a listbox but I'm getting the following error:
files = self.fileList()
TypeError: 'list' object is not callable
How can I access this list if I can't call it? I tried to use it as a global variable but maybe I was using it incorrectly. I want to be able to take items from that listbox and when a button is pressed, add them to another listbox.
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
I changed files to the following to see if files was setting properly and it returned an empty array
files = self.fileList
print files
#prints []
Upvotes: 0
Views: 1018
Reputation: 4604
You never initialise self.fileList
(nor fileListSorted
).
When you write in directoryContents
global fileList
fileList = Listbox(yscrollcommand = scrollbar.set)
...
you work on a global variable called fileList
. You could either use self.fileList
everywhere (or add global fileList
in all your function, and thus use fileList
).
However, I am skeptical of your use of classes, you should try to understand object-oriented concepts and their implementation in python, or ignore these concepts for the moment.
Edit
I have tried to run your code and you might also change the line
insertName = self.fileList[insertValue]
by
insertName = self.fileList.get(insertValue)
fileList
i a widget and every Tkinter widgets use dictionnary notation for properties (such as self.fileList['background']
).
Note that get take either a number, or a string containing a number and thus your conversion on above line is useless. Also note that you can get the whole list through get(0,END)
.
Upvotes: 1