473,320 Members | 2,006 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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
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
2 1676
bartonc
6,596 Expert 4TB
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
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

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

Similar topics

9
by: Yaroslav Bulatov | last post by:
I made an array of 10 million floats timed how long it takes to sum the elements, here's what I got (millis): gcc -O2: 21 Python with numarray: 104 Python with Numeric: 302...
13
by: Don Sealer | last post by:
I have about 40 different defects I'm tracking. I'd like to have all of these defects totaled individually, both by month and by year. I'd like to show these results in a report. I know I could...
6
by: John Ruiz | last post by:
Greetings, I originally posted this to microsoft.public.dotnet.framework.aspnet.datagridcontrol two weeks ago, but no one was able to answer. I am unable to dynamically add columns to a...
8
by: pmud | last post by:
Hi, I have 2 questions: 1. I have an editable data grid with 21 columns. I need to edit only 2 cloumns in this data grid. But when the grid is displayed in Edit mode, all the columns show long...
2
by: NowItsWhatever | last post by:
In query DESIGN view, how do I automatically "fit" the columns in the table/field grid to the lengths of the table and field names (including any functions applied to the fields). I am not talking...
0
by: bappelsin | last post by:
Hello, I have a problem with the datagridview. I have a view with around 25 different columns. To make life easier for the users I have implemented a popup dialog where the user can select which...
9
by: Earl | last post by:
I have somewhat of an interesting scenario: The form allows the user to select a service, which populates a a grid of product information related to that service ("service grid"). The user can...
7
by: lethek39 | last post by:
Hey I have been trying to figure out how to sum rows and columns in a matrix square. I also have been trying to get the program to list the numbers of the diagonal in the matrix. So far this is the...
1
by: msgjunkie | last post by:
I have 3 related tables in a DataSet - Customers, Products and Orders. I am trying to create an "editable" DataGridView which, ideally, will contain columns from all the tables. I have tried...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.