제목에서 한 목록 상자에서 항목을 선택하려고 함을 의미하는 단추를 누르고 두 번째 목록 상자에 추가하십시오.tkinter를 사용하여 한 목록 상자에서 다른 목록 상자로 항목 추가하기
이동할 단추를 클릭하면 값이 명령 프롬프트에 인쇄되지만 목록 상자 자체는 업데이트되지 않습니다.
나는 모든 것을 하나의 탭으로 탭해야한다는 것을 알기에 복사하여 붙였다.
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 = []
#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, padx = 2, pady=100)
fileList = Listbox(yscrollcommand = scrollbar.set) #files displayed in the left listBox
for filename in os.listdir(directory):
fileList.insert(END, filename)
global files
self.files.append(filename) #insert the values into the files array so we know which element we want to enter in moveFile
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)
##moveFile addes files to the array fileLIst2, which is the fileList on the right
def moveFile(self,File):
insertValue = int(File[0]) #convert the item to integer
global files
insertName = self.files[insertValue] #get the name of the file to be inserted
global fileListSorted
self.fileListSorted.append(str(insertName)) #append the value to the fileList array
print self.fileListSorted #second listbox list
이 코드는 클래스에 중첩되어 있습니까? 전역 변수와 멤버 메소드에는 이상한 혼합이 있습니다. 나는 당신이 리팩토링해야한다고 생각합니다 : 전역을 제거하고 모든 것을 클래스에 넣으십시오. – luc
예, 클래스에 있습니다. 전체 클래스로 다시 게시 해 드리겠습니다. – user1104854
코드 탭이 필요하다는 것을 알고 계시다면 왜하지 않으시겠습니까? 당신이 묻는 질문의 질에 자부심을 가져라. –