2013-04-15 1 views
0

.txt 파일의 내용을 여러 줄 textctrl로로드하는 응용 프로그램이 있습니다.이 textctrl에 대해 'for 루프'를 수행하고 각 줄의 텍스트를 가져오고 싶습니다. 추가 조작을 위해 배열에 넣으십시오.wxpython은 개행 문자로 구분 된 textctrl의 각 루프에 대해

이렇게 할 수 있습니까? 또는 파일을 열어 텍스트를 배열로 읽어서 textctrl에 표시하는 것이 더 좋을까요?

그렇다면 파일의 텍스트를 배열에 직접 입력하는 방법은 무엇입니까?

def OnOpen(self, e): 
    dlg = wx.FileDialog(self, "Choose a file to open", self.dirname, "", "*.txt", wx.OPEN) #open the dialog boxto open file 
    if dlg.ShowModal() == wx.ID_OK: #if positive button selected.... 
     directory, filename = dlg.GetDirectory(), dlg.GetFilename() 
     self.filePath = '/'.join((directory, filename))  #get the directory and filename of where file is located 
     directory, filename = dlg.GetDirectory(), dlg.GetFilename() 
     #self.urlFld.LoadFile(self.filePath) 
     self.fileTxt.SetValue(self.filePath) 

답변

1

난 그냥 빈 목록을 작성하고 파일에서 줄을 추가합니다 : 당신이 이미하고 있던으로

myList = [] 
for line in myOpenFileObj: 
    myList.append(line) 

그런 다음 텍스트 컨트롤에 텍스트를 추가합니다.

+0

내 배열이 있지만 텍스트의 마지막 줄만 인쇄합니다. .txt 문서에 3 줄의 텍스트가 있습니다. 앞에서 언급 한대로이 배열을 열고 "\ n"에 나눠서 textctrl에 쓰지 만 마지막 줄만 textctrl에 나타납니다. – jerrythebum

+0

DEF checkBtnClick (자체, 예)에 대한 I urlList = self.myList 을 (urlList) i.split에 J ("\ n")를위한 : self.urlFld.SetValue (j)는 – jerrythebum

+0

SetValue를 바로 덮어 컨트롤의 전체 값 대신 AppendText를 사용해보십시오 : http://wxpython.org/docs/api/wx.TextCtrl-class.html –

관련 문제