Take the 2-minute tour ×

I'm trying to select an item from a listbox, and when a button is pressed, that file's index position is passed to another function.

Right now I'm just trying to properly select the file but it's not working. A directory is selected via openfile (yes, bad naming and all)and then it is passed to directoryContents. From here "()" is printed in the command prompt. This should only happen when the button is pressed not immediately as it runs, so that's part of my problem. Upon selecting an item in the list and pressing the button, nothing happens.

Say I select the first item in the list and press the button, (0) should be printed in the command prompt.

class Actions:

    def openfile(self):
        directory = tkFileDialog.askdirectory(initialdir='.')

    def filename(self):
        Label (text='Please select a directory').pack(side=TOP,padx=10,pady=10)

    def directoryContents(self, directory):
        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, padx = 2)

        fileList = Listbox(yscrollcommand = scrollbar.set) #files displayed in the first scrollbar
        for filename in os.listdir(directory):
            fileList.insert(END, filename)
        fileList.pack(side =LEFT, fill = BOTH)
        scrollbar.config(command = fileList.yview)

        fileList2 = Listbox(yscrollcommand = scrollbarSorted.set) #second scrollbar (button will send selected files to this window)
        fileList2.pack(side =RIGHT, fill = BOTH)
        scrollbarSorted.config(command = fileList2.yview)

        selection = fileList.curselection() #select the file
        b = Button(text="->", command=self.moveFile(selection)) #send the file to moveFile
        b.pack(pady=5, padx =20)


    def moveFile(self,File):

#b = Button(text="->", command=Actions.moveFile(Actions.selection))
#b.pack(pady=5, padx =20)
share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

One problem that I see pretty quickly is:


should be something like:

b = Button(text="->", command=lambda:self.moveFile(fileList.curselection()))

As it is written, you're passing the result of self.moveFile as command (which obviously needs to call self.movefile in order to get the result)

Slipping a lambda in there defers calling the function until the button is actually clicked.

The mainloop in there also seems a little fishy ...

share|improve this answer
Adding lambda works, but something is wrong with my selection. It's only printing () –  user1104854 Oct 23 '12 at 18:12
@user1104854 -- Updated again. That was a related problem. You were setting selection to some value when you created the listbox. You weren't updating that selection when you clicked the button, so you were just grabbing an empty selection. –  mgilson Oct 23 '12 at 18:17
That was it, thanks! –  user1104854 Oct 23 '12 at 18:21
add comment

Your Answer


