2014-03-04 43 views
1

如何檢測listctrl中的重複值?我想知道如果我輸入值,如果它是重複的,計數器將加1,並不會在列表中添加另一行。希望有人能幫忙。我仍然是一個超級noob。 :dwxPython Listctrl計數器

import wx 

######################################################################## 
class MyForm(wx.Frame): 

    #---------------------------------------------------------------------- 
    def __init__(self): 
     wx.Frame.__init__(self, None, wx.ID_ANY, "List Control Tutorial") 

     # Add a panel so it looks the correct on all platforms 
     panel = wx.Panel(self, wx.ID_ANY) 
     self.index = 0 
     self.text_ctrl = wx.TextCtrl(panel) 

     self.list_ctrl = wx.ListCtrl(panel, size=(-1,100), 
         style=wx.LC_REPORT 
         |wx.BORDER_SUNKEN 
         ) 
     self.list_ctrl.InsertColumn(0, 'Line') 
     self.list_ctrl.InsertColumn(1, 'Number') 
     self.list_ctrl.InsertColumn(2, 'Count', width=125) 

     btn = wx.Button(panel, label="Add Line") 
     btn2 = wx.Button(panel, label="Get Column 1") 
     btn.Bind(wx.EVT_BUTTON, self.add_line) 
     btn2.Bind(wx.EVT_BUTTON, self.getColumn) 

     sizer = wx.BoxSizer(wx.VERTICAL) 
     sizer.Add(self.text_ctrl,0,wx.ALL|wx.EXPAND,5) 
     sizer.Add(self.list_ctrl, 0, wx.ALL|wx.EXPAND, 5) 
     sizer.Add(btn, 0, wx.ALL|wx.CENTER, 5) 
     sizer.Add(btn2, 0, wx.ALL|wx.CENTER, 5) 
     panel.SetSizer(sizer) 

    #---------------------------------------------------------------------- 
    def add_line(self, event): 
     line = "Line %s" % self.index 
     self.list_ctrl.InsertStringItem(self.index, line) 
     self.list_ctrl.SetStringItem(self.index, 1, self.text_ctrl.GetValue()) 
     self.index += 1 

    #---------------------------------------------------------------------- 
    def getColumn(self, event): 
     """""" 
     count = self.list_ctrl.GetItemCount() 
     for row in range(count): 
      item = self.list_ctrl.GetItem(itemId=row, col=1) 
      print item.GetText() 

#---------------------------------------------------------------------- 
# Run the program 
if __name__ == "__main__": 
    app = wx.App(False) 
    frame = MyForm() 
    frame.Show() 
    app.MainLoop() 

回答

0

夫婦的選擇,但更容易的選擇就是保持你放什麼ListCtrl軌道:

class MyForm(wx.Frame): 

    #---------------------------------------------------------------------- 
    def __init__(self): 
     wx.Frame.__init__(self, None, wx.ID_ANY, "List Control Tutorial") 

     self.listitems = set() 
     # or [] in cases where the items aren't hashable 

     .... 

    def add_line(self, event): 
     textval = self.text_ctrl.GetValue() 
     if textval not in self.listitems: 
      line = "Line %s" % self.index 
      self.list_ctrl.InsertStringItem(self.index, line) 
      self.list_ctrl.SetStringItem(self.index, 1, self.text_ctrl.GetValue()) 
      self.index += 1 
      self.listitems.add(textval) 

+0

感謝。指出我正確的方向。 – kylev