我正在尝试从列表框中选择一个项目,当按下按钮时,该文件的索引位置将传递给另一个函数。
现在我只是想正确选择文件,但它不起作用。通过 openfile 选择一个目录(是的,错误的命名和所有),然后将其传递给 directoryContents。从这里在命令提示符中打印“()”。这应该只在按钮运行时不立即按下时发生,所以这是我的问题的一部分。在列表中选择一个项目并按下按钮后,没有任何反应。
假设我选择列表中的第一项并按下按钮,(0)应该打印在命令提示符中。
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)