Reputation: 2167
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='.')
self.directoryContents(directory)
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)
mainloop()
def moveFile(self,File):
print(File)
#b = Button(text="->", command=Actions.moveFile(Actions.selection))
#b.pack(pady=5, padx =20)
Upvotes: 0
Views: 770
Reputation: 309929
One problem that I see pretty quickly is:
b = Button(text="->", command=self.moveFile(selection))
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 ...
Upvotes: 3