2010-01-04 4 views
1

체크 된 문자열 또는 체크리스트에서 정수를 가져 오려고합니다. 나는 그것을 어디에서나 얻을 수는 없다. 아래의 코드에서는 un-commented 코드 묶음을 볼 수 있습니다. 나는 어떤 제안이 그것과 관련이있을 때를 대비하여 나는 그들을 떠날 것이라고 생각했다. 나는 GUI 프로그래밍과 wx에있어서 아주 새로운 것이다. 당신의 도움을 주셔서 감사합니다.wxpython의 checklistbox에서 데이터를 가져 오는 방법을 이해할 수 없습니다.

import wx 


class Panel1(wx.Panel): 
def __init__(self, parent, log): 
    wx.Panel.__init__(self, parent, -1) 

    allLoc = ['One', 'Two', 'Three', 'Four'] 

    wx.StaticText(self, -1, "Choose:", (45, 15)) 

    citList = wx.CheckListBox(self, -1, (60, 50), wx.DefaultSize, allLoc) 

    #self.Bind(wx.EVT_CHECKLISTBOX, self.GetChecks, citList) 

    #h = citList.GetChecked() 

    #cities =() 

    #v = cities.append(h) 
    #print h 
    pos = citList.GetPosition().x + citList.GetSize().width + 25 
    nextBtn = wx.Button(self, -1, "Next", (pos, 50)) 
    self.Bind(wx.EVT_BUTTON, wx.EvyRadBox2, nextBtn) 

#def GetChecks(self, event): 
    #r = citList.GetId() 
    #print r 

#def printChecks(self, event): 
    #l = event.CetCheckStrings() 
    #print l 

def EvyRadBox2(self, event): 
    filepath2 = "c:\\logger2.txt" 
    file1 = open(filepath2, 'w') 
    file1.write('%d \n' % event.GetChecked()) 
    file1.close() 





app = wx.PySimpleApp() 
frame = wx.Frame(None, -1, "Charlie") 
Panel1(frame,-1) 
frame.Show(1) 
app.MainLoop() 

************************** 편집 **************** ****************

그래서 내가 처음에 이런 짓을이

import wx 

checkedItems = [] 


class citPanel(wx.Panel): 
def __init__(self, parent, id): 
    wx.Panel.__init__(self, parent, id) 
    allLoc = ['One', 'Two', 'Three', 'Four'] 

    wx.StaticText(self, -1, "Choose:", (45, 15)) 

    citList = wx.CheckListBox(self, -1, (60, 50), wx.DefaultSize, allLoc) 

    checkedItems = [i for i in range(citList.GetCount()) if citList.IsChecked(i)] 


class nextButton(wx.Button): 
def __init__(self, parent, id, label, pos): 
    wx.Button.__init__(self, parent, id, label, pos) 




class checkList(wx.Frame): 
def __init__(self, parent, id, title): 
    wx.Frame.__init__(self, parent, id, title, size=(400, 400)) 

    panel = citPanel(self, -1) 

    nextButton(panel, -1, 'Ok', (275, 50)) 

    self.Bind(wx.EVT_BUTTON, self.Clicked) 

    self.Centre() 
    self.Show(True) 

def Clicked(self, event): 
    print checkedItems 
    event.Skip() 


app = wx.App() 
checkList(None, -1, 'Charlie') 
app.MainLoop() 

처럼 내 코드를 변경, 내가 버튼을 클릭하면, 그것은을 wxstdout에 정의되지 않은 전역 이름을 던졌습니다. 지금은 빈 목록을 보여줍니다, 나는 상단에있는 checkedlist을 추가, 그것은 처음에는 아무도 없었다., 어떤 도움을, 언제나, 감사를해야한다 미리

답변

3
checkedItems = [i for i in range(citList.GetCount()) if citList.IsChecked(i)] 

citList.GetChecked() 또한 당신을위한 작업을 완료했다. __init__에서 선택한 항목을 가져 오는 중 문제가 생길 수 있습니다.

UPD : 당신은 __init__ 동안 항목을 검사받을 싶지 않아 - 그들은 그 순간에 사용자가 확인할 수 없습니다. 어떤 이벤트 핸들러에서든 확인하는 것이 좋습니다. wx.EVT_BUTTON.

는 : 예컨대, 더 자주

self.citList = wx.CheckListBox(self, -1, (60, 50), wx.DefaultSize, allLoc) 
# some code 
self.panel = citPanel(self, -1) 

self를 작성 시도에 Clicked 변경 :이 도움이

def Clicked(self, event): 
    checkedItems = [i for i in range(self.panel.citList.GetCount()) if self.panel.citList.IsChecked(i)] 
    print checkedItems 
    event.Skip() 

희망.

+0

단추에 바인딩 할 수 있습니까? – Kevin

+0

물론,'self.Bind (wx.EVT_BUTTON, self.EvyRadBox2, nextBtn)'와 비슷한 것을 쓸 수 있습니다 ('self.EvyRadBox2'에서 체크 된 아이템을 얻고 있다고 가정). – Li0liQ

+0

콘솔을 연 상태에서 실행하면 볼 수있는 것은 빈 튜플()입니다. 나는 수표가 읽히고 있는지, 나는 그들을 다른 것으로 전하기를 원할 것인가, 나는 단지 그들을 붙잡고 있다고 생각하지 않는다. – Kevin

관련 문제