Hi guys
I am having a little trouble with the format of numeric fields in a wxGrid
using Boa and wxPython 2.3.
There are a couple of fields which are automatically calculated, and I wish
to have them displayed with a fixed number of decimals. They however display
with a whole bunch of decimals. When I click twice on the cell (not tab into
or double click), the correct display appears, and when focus is lost the
display reverts to the max number of decimals.
Code is based on GridCustTable in the demo, and the WXGRID_VALUE_FLOAT
datatype.
Anyone got any ideas on a fix?
Regards
Lynndon Harnell
Code snippet is as follows.----------------------------------
#Boa:Frame:wxFrame2
from wxPython.wx import *
from wxPython.lib.anchors import LayoutAnchors
from wxPython.lib.bcrtl.user.StaticTextCtrl import *
from wxPython.grid import *
from wxPython.lib.buttons import *
from math import *
import string
global TotVol
global sPsmys
TotVol=0
SPsmys=1000000
ColHeaders=["ID","Len(m)","OD(mm)","WT(mm)","Gr","SMYS","Vol(m3)","Psmys"]
RowHeaders=["1","2"]
NCols=len(ColHeaders)
NRows=len(RowHeaders)
PipeGridData = [
[1, 30000, 610.0, 9.53, "X-70", 483,0,0],
[2, 10000, 610.0, 9.53, "X-70",483,0,0]
]
def GridUpdate(GridData):
z=len(GridData)
for i in range(0,z):
GridData[i][0]=i+1
# calc volume
length = GridData[i][1]
temp1 = GridData[i][3]
temp1 = float(temp1) / 1000 # wall thickness in m
idia = float(GridData[i][2]) / 1000
idia = idia - 2 * temp1
vol = length * pi/4
vol = vol * idia * idia # in m3
GridData[i][6] = vol
# calc SMYS
if GridData[i][4] == "B":
GridData[i][5] = 241
elif GridData[i][4][0:1] =="X":
GridData[i][5] = int(float(GridData[i][4][2:4]) * 6.895)
# calc Psmys
Psmys = 2 * temp1 * GridData[i][5]
if idia <> 0:
Psmys = Psmys / idia # in MPa
GridData[i][7] = Psmys
def CalcVol(GridData,Tvol):
Tvol = 0
z=len(GridData)
for i in range(0,z):
Tvol = Tvol + GridData[i][6]
print "CalcVol ",Tvol
return Tvol
def CalcPsmys(GridData,mPsmys):
mPsmys = 1000000
z=len(GridData)
for i in range(0,z):
if GridData[i][7] <> 0:
mPsmys = min(mPsmys,GridData[i][7])
print "CalcPsmys ",mPsmys
return mPsmys
GridUpdate(PipeGridData)
TotVol = CalcVol(PipeGridData,TotVol)
SPsmys = CalcPsmys(PipeGridData,SPsmys)
···
#---------------------------------------------------------------------------
class PipesTable(wxPyGridTableBase):
"""
"""
def __init__(self):
wxPyGridTableBase.__init__(self)
self.colLabels = ColHeaders
NCols=len(self.colLabels)
NRows=len(RowHeaders)
self.dataTypes = [wxGRID_VALUE_NUMBER,
wxGRID_VALUE_NUMBER,
wxGRID_VALUE_CHOICE +
':60.3,88.9,114.3,168.3,219.1,273.1,323.9,355.6,406.4,457.0,508.0,559.0,610.
0,660.0,711.0,762.0,813.0,864.0,914.0,1067.0',
wxGRID_VALUE_FLOAT + ':4 , 2',
wxGRID_VALUE_CHOICE +
':B,X-42,X-48,X-52,X-56,X-65,X-70,X-80',
wxGRID_VALUE_NUMBER,
wxGRID_VALUE_FLOAT + ':6 , 3',
wxGRID_VALUE_FLOAT + ':6, 3']
self.data = PipeGridData
#--------------------------------------------------
# required methods for the wxPyGridTableBase interface
def GetNumberRows(self):
return len(self.data)
def GetNumberCols(self):
return len(self.data[0])
def IsEmptyCell(self, row, col):
return not self.data[row][col]
def GetValue(self, row, col):
try:
return self.data[row][col]
except IndexError:
return ''
def SetValue(self, row, col, value):
try:
self.data[row][col] = value
except IndexError:
# add a new row
print "error"
#--------------------------------------------------
def GetColLabelValue(self, col):
return self.colLabels[col]
def GetTypeName(self, row, col):
return self.dataTypes[col]
def CanGetValueAs(self, row, col, typeName):
colType = string.split(self.dataTypes[col], ':')[0]
if typeName == colType:
return true
else:
return false
def CanSetValueAs(self, row, col, typeName):
return self.CanGetValueAs(row, col, typeName)
# -----------------------------------------
class PipesGrid(wxGrid):
def
__init__(self,parent,id=-1,pos=wxDefaultPosition,size=wxDefaultSize,style=wx
WANTS_CHARS,name="grid"):
wxGrid.__init__(self, parent, id, pos, size, style, name)
self.table = PipesTable()
self.SetTable(self.table, true)
self.SetRowLabelSize(0)
self.SetMargins(0,0)
self.SetColLabelSize(18)
self.AutoSizeColumns(false)
self.SetDefaultCellAlignment(wxALIGN_RIGHT,wxALIGN_CENTER)
for i in range(NCols):
self.SetColLabelValue(i,ColHeaders[i])
self.SetColSize(i,50)
self.SetColSize(0,20)
self.SetColSize(5,40)
self.SetColSize(6,60)
def SetData(self,data):
for row in range(len(data)):
for col in range(len(data[row])):
self.SetCellValue(row,col,data[row][col])
self.SetCellValue(row,1,row)
# ----------------------------------------------