Summing columns in a wx.Grid; Results in wx.TextCtrls

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ApoorvaDesai
    New Member
    • Sep 2007
    • 12

    Summing columns in a wx.Grid; Results in wx.TextCtrls

    I am using Boa constructor and python2.3

    I have a grid with lets say 3 columns and 25 rows and all the cells have numbers

    I want to add contents of column 1 and display in textctrl1, total of column 2 in text ctrl 2 and total of column 3 in textctrl3

    What i am doing right now is as follows
    Code:
    Number_of_rows_Grid1 = self.grid1.GetNumberRows()
    total = 0
    for all_rows_in_grid1 in range (0,Number_of_rows_Grid1):
                cell_value = self.grid1.GetCellValue(all_rows_in_grid1, 1)
                total = total + cell_value
                string_total = str(total)            
    self.textCtrl1.Value = string_total
    I repeat the same process for column 2 and column 3 and assign them to textctrl2 and textctrl3 respectively

    But If I want to do the following how do i assign the totals to the respective text ctrl
    Code:
    Number_of_rows_Grid1 = self.grid1.GetNumberRows()
    total = 0
     for all_columns_in_grid1 in range (0,3):
         for all_rows_in_grid1 in range (0,Number_of_rows_Grid1):
                cell_value = self.grid1.GetCellValue(all_rows_in_grid1, 1)
                total = total + cell_value
                string_total = str(total)
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by ApoorvaDesai
    I am using Boa constructor and python2.3

    I have a grid with lets say 3 columns and 25 rows and all the cells have numbers

    I want to add contents of column 1 and display in textctrl1, total of column 2 in text ctrl 2 and total of column 3 in textctrl3

    What i am doing right now is as follows
    Code:
    Number_of_rows_Grid1 = self.grid1.GetNumberRows()
    total = 0
    for all_rows_in_grid1 in range (0,Number_of_rows_Grid1):
                cell_value = self.grid1.GetCellValue(all_rows_in_grid1, 1)
                total = total + cell_value
                string_total = str(total)            
    self.textCtrl1.Value = string_total
    I repeat the same process for column 2 and column 3 and assign them to textctrl2 and textctrl3 respectively

    But If I want to do the following how do i assign the totals to the respective text ctrl
    Code:
    Number_of_rows_Grid1 = self.grid1.GetNumberRows()
    total = 0
     for all_columns_in_grid1 in range (0,3):
         for all_rows_in_grid1 in range (0,Number_of_rows_Grid1):
                cell_value = self.grid1.GetCellValue(all_rows_in_grid1, 1)
                total = total + cell_value
                string_total = str(total)
    Oh boy, sounds like fun! You may not be aware that it's OK to add your own stuff to the __init__() function of a Boa generated Frame, etc. I use this trick often:[CODE=python]# Boa's widget layout above + next 2 lines
    def __init__(self, parent):
    self._init_ctrl s(parent)
    # your code here
    self.tcList = (self.textCtrl1 , self.textCtrl2, self.textCtrl3)

    def OnWhatever(self , event):
    for col in range (3): # range() starts at zero by default
    total = 0 # reset total for each column
    for row in range (Number_of_rows _Grid1): # I think it's more clear this way
    cell_value = self.grid1.GetC ellValue(row, col)
    total += cell_value # another shortcut
    string_total = str(total)
    self.tcList[col].SetValue(strin g_total)[/CODE]This is not tested, but should serve to give you the basic idea.
    Last edited by bartonc; Nov 20 '07, 03:14 AM.

    Comment

    • ApoorvaDesai
      New Member
      • Sep 2007
      • 12

      #3
      Thanks a lot.... the array idea was awesome... it did work beautifully.
      Redused about 115 lines of code to 20 lines.

      Code:
      Number_of_rows_Grid1 = self.grid1.GetNumberRows()
              
              self.tcList = (self.textCtrl1, self.textCtrl2, self.textCtrl4, self.textCtrl5, self.textCtrl7, self.textCtrl8)
              col_list = (1, 2, 4, 5, 7, 8)
              
              for col_index in range (0,6):
                  total = 0    
                  for all_rows_grid1 in range(0,Number_of_rows_Grid1-1):
                      cell_value = self.grid1.GetCellValue(all_rows_grid1, col_list[col_index])
                      if cell_value:
                          cell_value = int (cell_value)
                      else:
                          cell_value = 0
                      
                      total = total + cell_value
                      self.tcList[col_index].SetValue(str(total))
              
              self.textCtrl3.Value =  str(round(float(self.textCtrl2.Value)*100.00/float(self.textCtrl1.Value),2))
              self.textCtrl6.Value =  str(round(float(self.textCtrl5.Value)*100.00/float(self.textCtrl4.Value),2))
              self.textCtrl9.Value =  str(round(float(self.textCtrl8.Value)*100.00/float(self.textCtrl7.Value),2))

      Comment

      Working...