Commans in Numbers

18 views
Skip to first unread message

yoav glazner

unread,
Nov 12, 2009, 6:09:38 AM11/12/09
to wxpytho...@googlegroups.com
Hi All,

I want to display the numbers in a grid with Commas.

here is a small example on how it doesn't work 

import  wx
import  wx.grid as gridlib

#---------------------------------------------------------------------------
class CustomDataTable(gridlib.PyGridTableBase):
    def __init__(self):
        gridlib.PyGridTableBase.__init__(self)
        self.colLabels = ['number']
        self.dataTypes = [gridlib.GRID_VALUE_LONG,
                          ]
        self.data = [
            [1010],
            ]
   
    # required methods for the wxPyGridTableBase interface
    def GetNumberRows(self):
        return len(self.data)
    def GetNumberCols(self):
        return len(self.colLabels)
    
    def IsEmptyCell(self, row, col):
        try:
            return not self.data[row][col]
        except IndexError:
            return True
    def GetValue(self,row,col):
        return self.data[row][col]

    def SetValue(self, row, col, value):
        self.data[row][col] = value
  
    def GetColLabelValue(self, col):
        return self.colLabels[col]
   
    def GetTypeName(self, row, col):
        return self.dataTypes[col]
  
    def CanGetValueAs(self, row, col, typeName):
        colType = self.dataTypes[col].split(':')[0]
        if typeName == colType:
            return True
        else:
            return False
        
    def CanSetValueAs(self, row, col, typeName):
        return self.CanGetValueAs(row, col, typeName)
    
if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = wx.Frame(None,-1)
    grid = wx.grid.Grid(frame)
    grid.SetTable(CustomDataTable())
    frame.Show(True)
    app.MainLoop()

also i want to note i'm using windows 2003 python 2.6 , wxpython 2.8.10.1
and i really need to use PyGridTableBase since my real table has much more features in it


Thanks,
  Yoav

Mike Driscoll

unread,
Nov 12, 2009, 11:41:18 AM11/12/09
to wxPython-users
I think you'll need to create a custom cell renderer and/or cell
editor to accomplish this. You may be able to do it using cell focus
events such that when the cell loses focus, you reset the value to
include commas.

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org

yoav glazner

unread,
Nov 13, 2009, 3:48:31 AM11/13/09
to wxpytho...@googlegroups.com
I think you'll need to create a custom cell renderer and/or cell
editor to accomplish this. You may be able to do it using cell focus
events such that when the cell loses focus, you reset the value to
include commas.

-------------------
Mike Driscoll

 is there any example on how can i do it?

Mike Driscoll

unread,
Nov 13, 2009, 10:35:59 AM11/13/09
to wxPython-users
In the demo package, there's a grid example that has a custom renderer
in it. See the "wx.Grid showing Editors and Renderers". You'll have to
open the Python file to see how it's done.

- Mike

Raphael Mayoraz

unread,
Nov 13, 2009, 11:43:08 AM11/13/09
to wxpytho...@googlegroups.com
Hello,

If I understand well your request, this is a function that may help you,
or give
an idea how this can be done. I'm still a beginner with all this stuff,
so ... be
careful. It does use locale to find out which separator character should
be used
(as one part of the world is using ' and not ,). You would call the
function
from the EndEdit method of your cell editor class.
Example:

#-----------------------------------------------
...
import locale
...
# Treat locale (getting the character for thousands separator)
locale.setlocale(locale.LC_ALL, '') # It is required to override the default
thousep = locale.localeconv() # Creates a dictionary with all
locale infos in
thousands_sep = thousep['thousands_sep']
...
...
class myCellEditor(wx.grid.PyGridCellEditor):
def __init__(self, thousands_sep):
wx.grid.PyGridCellEditor.__init__(self)
self.thousands_sep = thousands_sep
...

def EndEdit(self, row, col, grid):
self.value = groupChars(self.value, self.thousands_sep) # Runs
thousands seperator thing
...
...

def groupChars(value, thousands_sep):
''' Function = groupChars, args = value, thousands_sep
Add either a ' or a , each 3 characters.
This is used for large numbers typically entered in a numeric field.
Example: entering 1234567.89 will return 1,234,567.89 depending
on the value of thousands_sep.
value: must be a string

thousands_sep: should be either , or ', depending on the locale.
locale should be defined at the begining of the
application.

returns: a string in the here above described format.

WARNING! the function doesn't check that "value" is a number.
the function doesn't change the decimal . or ,
that varies from locale
'''

parts = str(value).split('.')
value = parts[0]
chunks = []
while len(value) > 0:
if len(value) > 3:
chunks.insert(0, value[-3:])
value = value[:-3]
else:
chunks.insert(0,value)
value = ''
parts[0] = thousands_sep.join(chunks) # thousands_sep comes from locale
return '.'.join(parts) # not supporting decimal
locale, using .

#----------------------------------------------------------------

Hope this helps,

Raphael


Mike Driscoll

unread,
Nov 13, 2009, 2:25:53 PM11/13/09
to wxPython-users
Hi,
Raphael reminded me that we had done something like this with the
locale module too. Here's how we use it in the USA:

<interpreter results>

>>> import locale
>>> locale.setlocale(locale.LC_ALL, "English_United States.1252")
'English_United States.1252'
>>> locale.currency(4, grouping=True)
'$4.00'
>>> locale.currency(4000, grouping=True)
'$4,000.00'

</interpreter>

You may be able to use this to get you going.
Reply all
Reply to author
Forward
0 new messages