2012-07-26 63 views
0

我试图使用清单控件的焦点来向用户提供信息以帮助他们进行选择。我可以self.list.GetFocus检查项目事件,但似乎无法获得检查项目之外的焦点。 我的目标是让用户选项卡通过列表,并且随着列表的焦点更改,消息从焦点行消息状态栏信息,以便通知用户。 OnCheckItem选择用于触发另一个操作。 任何人都可以给我一些方向吗? 感谢什么事件可以触发wx.Checklist控件的GetFocus?

下面是我使用的代码:

#!/usr/bin/env python 
    # encoding: ISO-8859-1 
    """ 
    Simple Checkbox List Control.py 

    """ 

    import wx 
    import wx.lib.mixins.listctrl as listmix 
    from wx.lib.pubsub import Publisher 


    listhdr=('File', 'Info','Data') 
    listdata=( ("file00","info00","data0"), 
       ("file01","info01","data1"), 
       ("file02","info02","data2"), 
       ("file03","info03","data3"), 
       ("file04","info04","data4"), 
       ("file05","info05","data5")) 

    #======================================================================= 
    #   CheckListControl Panel Class 
    #======================================================================= 

    class TestListCtrl(wx.ListCtrl, listmix.CheckListCtrlMixin, listmix.ListCtrlAutoWidthMixin): 
     def __init__(self, *args, **kwargs): 
      wx.ListCtrl.__init__(self, *args, **kwargs) 
      # Initialize ListCtrl Widge 
      listmix.CheckListCtrlMixin.__init__(self) 
      listmix.ListCtrlAutoWidthMixin.__init__(self) 
      self.setResizeColumn(3) 

      self.checked = -1 # class container variable for the checked item index 
      self.rowindex=[] # class container variable for selected row 
      self.status={} # Dictionary to keep track of the checklist state 
      #print self.GetFocus() 
     def OnCheckItem(self, index, flag): 
      self.msg1='' 
      self.msg2='' 
      if flag==True: 
       #msg=str('Checked Item '+str(index)) 
       #The following insures only one checked item is checked at a time 
       if self.checked==-1: 
        self.checked=index 
        self.msg1='Checked box %i '%(self.checked) 
       else: 
        self.CheckItem(self.checked, False) 
        self.checked=index 
        self.msg1=' Checked box %i '%(self.checked) 
      if flag==False: 
       self.msg2=str('Unchecked Item '+str(index)) 
       #The following addresses checked item that was unchecked 
       self.checked=-1 
      self.status[index]=flag # set dictionary to current state of any changed item 
      #print 'status ',self.status 
      #msg='Check list Status: '+str(self.status) 
      msg=self.msg2+self.msg1 
      #print msg 
      Publisher().sendMessage(('change_statusbar'), msg) # Communication with Main Frame 
      print msg 
      self.Refresh() # this just to insure the displayed list is current 

    #======================================================================= 
    #   CheckList Panel Class 
    #=======================================================================    
    class CheckListPanel(wx.Panel): 
     def __init__(self, parent): 
      wx.Panel.__init__(self, parent, -1) 
      self.SetBackgroundColour('blue') 
      # Define CheckBoxList Layout 
      self.list = TestListCtrl(self, style=wx.LC_REPORT) # Instantiate the checklist control 
      self.list.InsertColumn(0, "File",width=100) 
      self.list.InsertColumn(1, "Info") 
      self.list.InsertColumn(2, "Data") 
      self.list.Arrange() 
      # Populate the List Table 
      for entry in listdata: 
       #self.list.Append([str(i), "", "It's the %d item" % (i)]) # this is from the source file template 
       self.list.Append(entry) # construct the table entries 

      # add CheckListPanel widgets to vertical sizer 
      self.sizer = wx.BoxSizer(wx.VERTICAL) 
      self.sizer.Add(self.list,1, flag=wx.EXPAND | wx.ALL, border=5) 
      self.SetSizerAndFit(self.sizer) 

    #=========================================================================================================================== 
    #   Main Frame 
    #=========================================================================================================================== 
    class MainWindow(wx.Frame): 
     def __init__(self, parent): 
      wx.Frame.__init__(self, parent, -1, 'Panel with CheckBox List Control-Mixins Example', size=(700, 600)) 
      self.sb=self.CreateStatusBar() 
      self.sb.SetStatusText("Check an Item") 
      Publisher().subscribe(self.change_statusbar, 'change_statusbar') 
      self.panel = wx.Panel(self) 
      self.list = CheckListPanel(self.panel) 

      # now add widgets to FramePanel 
      self.sizer = wx.BoxSizer(wx.VERTICAL) # create Sizer 
      self.sizer.Add(self.list,1, flag=wx.EXPAND | wx.ALL, border=5) 
      self.panel.SetSizer(self.sizer) # 

      self.Show() 

     # Set the methods to receive PubSub transfers 
     def change_statusbar(self, msg): 
      self.msg=msg 
      self.SetStatusText(msg.data)   

    app = wx.App(False) 
    win = MainWindow(None) 
    app.MainLoop() 

回答

0

嗯,我可能已经想通out.The下面的代码,当加到CheckListPanel类将允许所选择的项目被识别。我想在使用GetSelectedItem()方法之前,必须先实例化列表控件,因为它需要一个wx.ID来绑定事件。我在填充列表后添加了这个设置面板sizer

  # bind selected event to selected row 
    wx.EVT_LIST_ITEM_SELECTED(self,self.list.GetId(), self.OnSelectItem) 


def OnSelectItem(self, index): 
    print 'Selected item index=',self.list.GetFocusedItem() 
    msg='Selected item index='+str(self.list.GetFocusedItem()) 
    Publisher().sendMessage(('change_statusbar'), msg) # Communication with Main Frame 
相关问题