WxPYTHON GetValue from wxGrid HELP

M

matthiasjanes

dear all,

I just need a little help.

could anyone give real code example (simple) how to read the value of
a grid cell.

i could not figure it out and really would like to do a simple
spreadsheet application in wxpython (my first try with wxpython gui)

so, if any one knows just how to read the value of a cell - a real
working example would be greatly appriciated

Matthias Janes
 
L

Larry Bates

All you do is:

self.GetCellValue(row, column)

Where self is a wxGrid class instance.

Sample class from working program:

class SimpleGrid(wxGrid): ##, wxGridAutoEditMixin):
def __init__(self, parent):
wxGrid.__init__(self, parent, -1)
#
# Get shortcuts to some editors for different types of cells
#
bEditor=wxGridCellBoolEditor()
bRenderer=wxGridCellBoolRenderer()
tEditor=wxGridCellTextEditor()
tRenderer=wxGridCellStringRenderer()
fEditor=wxGridCellFloatEditor()
fRenderer=wxGridCellFloatRenderer()
fRenderer.SetPrecision(2)
#
# Define and set the column sizes and labels
#
# Display
#--Column Heading, Width, Alignment,read-only, editor, renderer
self.columndefs=(
("?", 20, wxALIGN_LEFT, false, bEditor, bRenderer),
("Cust#", 50, wxALIGN_LEFT, true, tEditor, tRenderer),
("Cust Name", 180, wxALIGN_LEFT, true, tEditor, tRenderer),
("Reference", 100, wxALIGN_LEFT, true, tEditor, tRenderer),
("DocType", 50, wxALIGN_CENTRE,true, tEditor, tRenderer),
("Inv. #", 75, wxALIGN_CENTRE,true, tEditor, tRenderer),
("InvDate", 60, wxALIGN_CENTRE,true, tEditor, tRenderer),
("L#", 35, wxALIGN_CENTRE,true, tEditor, tRenderer),
("Item #", 65, wxALIGN_CENTRE,true, tEditor, tRenderer),
("Description",300, wxALIGN_LEFT, false, tEditor, tRenderer),
("Qty", 50, wxALIGN_RIGHT, false, tEditor, tRenderer),
("Price", 50, wxALIGN_RIGHT, true, fEditor, fRenderer),
("Cost", 50, wxALIGN_RIGHT, true, fEditor, fRenderer),
("OMMC?", 60, wxALIGN_CENTRE,false, tEditor, tRenderer),
("OMMC $", 70, wxALIGN_RIGHT, false, tEditor, tRenderer),
("S#", 35, wxALIGN_CENTRE,false, tEditor, tRenderer),
("Serial #'s", 300, wxALIGN_LEFT, false, tEditor, tRenderer)
)

self.columncount=len(self.columndefs)
self.CreateGrid(0, self.columncount)
#
# Set column widths
#
map(self.SetColSize, range(self.columncount),
[a[1] for a in self.columndefs])

#
# Set custom column labels
#
map(self.SetColLabelValue, range(self.columncount),
[a[0] for a in self.columndefs])

#
# Sheet will toggle between these two colors on change of invoice
number
#
# LightGray
self._backgroundcolors=[wxWHITE,"#CCCCCC"]
self._toggleBGC=0
#
# Register event handlers
#
#EVT_GRID_CELL_LEFT_DCLICK(self, self.OnCellLeftDClick)
#EVT_GRID_CELL_LEFT_CLICK(self, self.OnCellLeftClick)
#EVT_GRID_CELL_RIGHT_CLICK(self, self.OnCellRightClick)
#EVT_GRID_RANGE_SELECT(self, self.OnRangeSelect)
EVT_GRID_CELL_CHANGE(self, self.OnCellChange)
return

def OnCellChange(self, evt):
row=evt.GetRow()
col=evt.GetCol()
#
# Special handlers for some columns
#
if col == 13:
self.SetCellValue(row, col, self.GetCellValue(row,col).upper())
if self.GetCellValue(row, col) not in ("Y","N"):
self.SetCellValue(row, col, "Y")
if self.GetCellValue(row, col) == "N":
self.SetCellTextColour(row, 13, wxBLACK)

if self.GetCellValue(row, col) == "Y" and \
self.GetCellValue(row, 14) == "0.00":
self.SetCellTextColour(row, col, wxRED)

elif col == 14:
if self._trace: self.logf.writelines("T","Col=14 functions")
equation=self.GetCellValue(row, col)
if equation[0] == "=": equation=equation[1:]
try: value="%.2f" % eval(equation)
except: value="0.00"
self.SetCellValue(row, col, value)
if value != "0.00" and self.GetCellValue(row, 13) == "Y":
self.SetCellTextColour(row, 13, wxBLACK)
elif value == "0.00" and self.GetCellValue(row, 13) == "Y":
self.SetCellTextColour(row, 13, wxRED)


elif col == 15:
self.SetCellValue(row, col, self.GetCellValue(row,col).upper())
if self.GetCellValue(row, col) not in ("Y","N"):
self.SetCellValue(row, col, "Y")
if self.GetCellValue(row, col) == "N":
self.SetCellTextColour(row, col, wxBLACK)
self.SetCellTextColour(row, 9, wxBLACK)

if self.GetCellValue(row, col) == "Y" and \
not self.GetCellValue(row, 16):
self.SetCellTextColour(row, col, wxRED)
self.SetCellTextColour(row, 9, wxRED)

else: return

self.ForceRefresh()
return



Hope it helps. I pretty much got this from the wxWindows demos
that come with standard installation.

Regards,
Larry Bates
Syscon, Inc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top