2014-03-04 50 views
0

我想自動計算事件發生後的總計列,而無需進行按鈕計算。我定義的函數總數的循環是錯誤的,我一直困住了幾個小時搞清楚什麼listctrl事件使用的思想,它應該autocompute在標籤的編輯和數據輸入通過添加行。請幫忙!我仍然是一個小菜鳥。謝謝! :)自動計算wxPython ListCtrl

import wx 
import wx.lib.mixins.listctrl as listmix 

######################################################################## 
class EditableListCtrl(wx.ListCtrl, listmix.TextEditMixin): 
    ''' TextEditMixin allows any column to be edited. ''' 

    #---------------------------------------------------------------------- 
    def __init__(self, parent, ID=wx.ID_ANY, pos=wx.DefaultPosition, 
       size=wx.DefaultSize, style=0): 
     """Constructor""" 
     wx.ListCtrl.__init__(self, parent, ID, pos, size, style) 
     listmix.TextEditMixin.__init__(self) 
     self.Bind(wx.EVT_LIST_BEGIN_LABEL_EDIT, self.OnBeginLabelEdit) 

    def OnBeginLabelEdit(self, event): 
     if event.m_col == 0: 
      event.Veto() 
     elif event.m_col == 4: 
      event.Veto() 
     else: 
      event.Skip() 

######################################################################## 
class MyPanel(wx.Panel): 
    """""" 

    #---------------------------------------------------------------------- 
    def __init__(self, parent): 
     """Constructor""" 
     wx.Panel.__init__(self, parent) 
     self.text_ctrl = wx.TextCtrl(self) 

     rows = [("Ford", "123", "1996", ""), 
       ("Nissan", "432", "2010", ""), 
       ("Porche", "911", "2009", "") 
       ] 
     self.list_ctrl = EditableListCtrl(self, style=wx.LC_REPORT) 

     self.list_ctrl.InsertColumn(0, "Somethin") 
     self.list_ctrl.InsertColumn(1, "Price") 
     self.list_ctrl.InsertColumn(2, "Qty") 
     self.list_ctrl.InsertColumn(3, "Total") 
     self.listitems = set() 
     self.index = 0 
     index = 0 
     for row in rows: 
      self.list_ctrl.InsertStringItem(index, row[0]) 
      self.list_ctrl.SetStringItem(index, 1, row[1]) 
      self.list_ctrl.SetStringItem(index, 2, row[2]) 
      self.list_ctrl.SetStringItem(index, 3, row[3]) 
      index += 1 
     btn = wx.Button(self, label="Add Line") 
     btn.Bind(wx.EVT_BUTTON, self.add_line) 
     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) 
     self.SetSizer(sizer) 
     self.Bind(wx.EVT_LIST_BEGIN_LABEL_EDIT, self.total) 
    def add_line(self, event): 
     textval = self.text_ctrl.GetValue() 
     if textval not in self.listitems: 
      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) 
     print "duplicate detected" 

    def total(self,event): 
     count = self.list_ctrl.GetItemCount() 
     for row in range(count): 
      itemprice = self.list_ctrl.GetItem(itemId=row, col=1) 
      itemqty = self.list_ctrl.GetItem(itemId=row, col=2) 
      itempriceval = itemprice.GetText() 
      itemqtyval = itemqty.GetText() 
      total = float(itempriceval)*float(itemqtyval) 
      self.list_ctrl.SetStringItem(count-1, 5, "%.2f" % total) 


######################################################################## 
class MyFrame(wx.Frame): 
    """""" 

    #---------------------------------------------------------------------- 
    def __init__(self): 
     """Constructor""" 
     wx.Frame.__init__(self, None, wx.ID_ANY, "Editable List Control") 
     panel = MyPanel(self) 
     self.Show() 

#---------------------------------------------------------------------- 
if __name__ == "__main__": 
    app = wx.App(False) 
    frame = MyFrame() 
    app.MainLoop() 
+0

如果你能弄清楚如何使用按鈕做的,拿到的工作,然後就創建事件編程方式將模擬用戶按下按鈕 – wnnmaw

回答

1

好吧,試試這個你MyPanel

class MyPanel(wx.Panel): 
    """""" 

    #---------------------------------------------------------------------- 
    def __init__(self, parent): 
     """Constructor""" 
     wx.Panel.__init__(self, parent) 
     self.text_ctrl = wx.TextCtrl(self) 

     rows = [("Ford", "123", "1996", ""), 
       ("Nissan", "432", "2010", ""), 
       ("Porche", "911", "2009", "") 
       ] 
     self.list_ctrl = EditableListCtrl(self, style=wx.LC_REPORT, size=(-1, 150)) 

     self.list_ctrl.InsertColumn(0, "Something") 
     self.list_ctrl.InsertColumn(1, "Price") 
     self.list_ctrl.InsertColumn(2, "Qty") 
     self.list_ctrl.InsertColumn(3, "Total") 
     self.listitems = set() 
     self.index = 0 
     index = 0 
     for row in rows: 
      self.list_ctrl.InsertStringItem(index, row[0]) 
      self.list_ctrl.SetStringItem(index, 1, row[1]) 
      self.list_ctrl.SetStringItem(index, 2, row[2]) 
      self.list_ctrl.SetStringItem(index, 3, row[3]) 
      index += 1 
     btn = wx.Button(self, label="Add Line") 
     btn.Bind(wx.EVT_BUTTON, self.add_line) 
     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) 
     self.SetSizer(sizer) 
     self.Bind(wx.EVT_LIST_END_LABEL_EDIT, self.total) 

    def add_line(self, event): 
     textval = self.text_ctrl.GetValue() 
     if textval not in self.listitems: 
      line = str(self.index) 
      self.list_ctrl.InsertStringItem(self.index, line) 
      self.list_ctrl.SetStringItem(self.index, 0, self.text_ctrl.GetValue()) 
      self.index += 1 
      self.listitems.add(textval) 
     else: 
      print "duplicate detected" 

    def total(self,event): 
     if not event.IsEditCancelled(): 
      count = self.list_ctrl.GetItemCount() 
      totals = [] 
      try: 
       for row in range(count): 
        itemprice = self.list_ctrl.GetItem(itemId=row, col=1) 
        itemqty = self.list_ctrl.GetItem(itemId=row, col=2) 
        itempriceval = itemprice.GetText() 
        itemqtyval = itemqty.GetText() 
        total = float(itempriceval)*float(itemqtyval) 
        totals.append(total) 

       print totals 

       for row, total in zip(range(count),totals): 
        self.list_ctrl.SetStringItem(row, 3, "%.2f" % total) 
      except: 
       return 
     else: 
      print "edit was cancelled" 

大部分我所做的就是在total(),最值得注意的是,try...except塊。當不是所有的總數都可以計算出來時,這樣可以避免發生錯誤。我在其他地方也改變了一些東西,比如綁定事件(現在基於編輯結束),並且一些索引也是錯誤的(你試圖將總數寫入索引5,應該是3)

希望這有助於

+0

感謝您的幫助。只需要更多的修復listctrl事件。 – kylev

+0

你需要什麼? – wnnmaw

+0

當我嘗試此操作時,EVT_LIST_END_LABEL_EDIT處理程序中的GetText()返回每列的預編輯值。 – Semprini