By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,172 Members | 727 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,172 IT Pros & Developers. It's quick & easy.

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

P: 12
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
Expand|Select|Wrap|Line Numbers
  1. Number_of_rows_Grid1 = self.grid1.GetNumberRows()
  2. total = 0
  3. for all_rows_in_grid1 in range (0,Number_of_rows_Grid1):
  4.             cell_value = self.grid1.GetCellValue(all_rows_in_grid1, 1)
  5.             total = total + cell_value
  6.             string_total = str(total)            
  7. 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
Expand|Select|Wrap|Line Numbers
  1. Number_of_rows_Grid1 = self.grid1.GetNumberRows()
  2. total = 0
  3.  for all_columns_in_grid1 in range (0,3):
  4.      for all_rows_in_grid1 in range (0,Number_of_rows_Grid1):
  5.             cell_value = self.grid1.GetCellValue(all_rows_in_grid1, 1)
  6.             total = total + cell_value
  7.             string_total = str(total)            
  8.  
Nov 20 '07 #1
Share this Question
Share on Google+
2 Replies


bartonc
Expert 5K+
P: 6,596
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
Expand|Select|Wrap|Line Numbers
  1. Number_of_rows_Grid1 = self.grid1.GetNumberRows()
  2. total = 0
  3. for all_rows_in_grid1 in range (0,Number_of_rows_Grid1):
  4.             cell_value = self.grid1.GetCellValue(all_rows_in_grid1, 1)
  5.             total = total + cell_value
  6.             string_total = str(total)            
  7. 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
Expand|Select|Wrap|Line Numbers
  1. Number_of_rows_Grid1 = self.grid1.GetNumberRows()
  2. total = 0
  3.  for all_columns_in_grid1 in range (0,3):
  4.      for all_rows_in_grid1 in range (0,Number_of_rows_Grid1):
  5.             cell_value = self.grid1.GetCellValue(all_rows_in_grid1, 1)
  6.             total = total + cell_value
  7.             string_total = str(total)            
  8.  
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:
Expand|Select|Wrap|Line Numbers
  1. # Boa's widget layout above + next 2 lines
  2.     def __init__(self, parent):
  3.         self._init_ctrls(parent)
  4. # your code here
  5.         self.tcList = (self.textCtrl1, self.textCtrl2, self.textCtrl3)
  6.  
  7.     def OnWhatever(self, event):
  8.         for col in range (3):  # range() starts at zero by default
  9.             total = 0  # reset total for each column
  10.             for row in range (Number_of_rows_Grid1): # I think it's more clear this way
  11.                 cell_value = self.grid1.GetCellValue(row, col)
  12.                 total += cell_value # another shortcut
  13.                 string_total = str(total) 
  14.             self.tcList[col].SetValue(string_total)
This is not tested, but should serve to give you the basic idea.
Nov 20 '07 #2

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

Expand|Select|Wrap|Line Numbers
  1. Number_of_rows_Grid1 = self.grid1.GetNumberRows()
  2.  
  3.         self.tcList = (self.textCtrl1, self.textCtrl2, self.textCtrl4, self.textCtrl5, self.textCtrl7, self.textCtrl8)
  4.         col_list = (1, 2, 4, 5, 7, 8)
  5.  
  6.         for col_index in range (0,6):
  7.             total = 0    
  8.             for all_rows_grid1 in range(0,Number_of_rows_Grid1-1):
  9.                 cell_value = self.grid1.GetCellValue(all_rows_grid1, col_list[col_index])
  10.                 if cell_value:
  11.                     cell_value = int (cell_value)
  12.                 else:
  13.                     cell_value = 0
  14.  
  15.                 total = total + cell_value
  16.                 self.tcList[col_index].SetValue(str(total))
  17.  
  18.         self.textCtrl3.Value =  str(round(float(self.textCtrl2.Value)*100.00/float(self.textCtrl1.Value),2))
  19.         self.textCtrl6.Value =  str(round(float(self.textCtrl5.Value)*100.00/float(self.textCtrl4.Value),2))
  20.         self.textCtrl9.Value =  str(round(float(self.textCtrl8.Value)*100.00/float(self.textCtrl7.Value),2))
Nov 20 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.