2012-10-24 4 views
0

제목에서 한 목록 상자에서 항목을 선택하려고 함을 의미하는 단추를 누르고 두 번째 목록 상자에 추가하십시오.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 
+1

이 코드는 클래스에 중첩되어 있습니까? 전역 변수와 멤버 메소드에는 이상한 혼합이 있습니다. 나는 당신이 리팩토링해야한다고 생각합니다 : 전역을 제거하고 모든 것을 클래스에 넣으십시오. – luc

+0

예, 클래스에 있습니다. 전체 클래스로 다시 게시 해 드리겠습니다. – user1104854

+0

코드 탭이 필요하다는 것을 알고 계시다면 왜하지 않으시겠습니까? 당신이 묻는 질문의 질에 자부심을 가져라. –

답변

1

그것은 그 코드를 따라하는 것은 매우 어렵습니다 - 예를 들어, 어디 self.fileListSorted 정의된다? - 세계 번호가 fileListSorted이고 인스턴스 변수가 self.fileListSorted이고 다른 것들입니다. 그러나 보이는 그들을 혼동 받고있는 것으로 또한 ListBox에 항목을 추가 할 점에 유의 당신이 일반적으로를 사용합니다 (예를 들어, 왜 줄 당신이 거기에 fileListSorted를 사용하지 않을 moveFile에서

global fileListSorted 

은?있다) insert 방법. moveFiles에서 사용하지 않은 방법은 ...

+0

삽입 (END, ...)을 사용해 보았지만 정수가 있어야한다는 오류가 발생했습니다. – user1104854

+0

미안하지만, 아직 초보자입니다. 특히 Python을 사용하는 OOP의 경우에 그렇습니다. self.fileListSorted를 fileLIst로 변경하면 작동합니다. – user1104854

+0

@ user1104854 - 아무런 문제가 없습니다. 기꺼이 도와 줘. :). 파이썬 배우기 - 꽤 강력한 언어입니다. – mgilson

관련 문제