Search code examples
python-3.xgridwxpythonwxwidgetswxgrid

wxpython phoenix: How to get float values from wx grid cells and perform mathematical operations?


I am trying to build a invoice with wx grid, I would like to add the values in quantity column and the values in price column and display it in the row total.

import wx
import wx.grid as gridlib


 class MyForm(wx.Frame):
   def __init__(self):
    wx.Frame.__init__(self, None, wx.ID_ANY, "Invoice")
    datag = 0


    # Add a panel so it looks the correct on all platforms
    panel = wx.Panel(self, wx.ID_ANY)
    grid = gridlib.Grid(panel)
    grid.CreateGrid(25, 3)
    note_sizer = wx.BoxSizer()
    note_sizer.Add(grid, 1, wx.EXPAND)
    panel.SetSizer(note_sizer)

    # We can set the sizes of individual rows and columns
    # in pixels
    grid.SetColSize(0, 520)

    # change a couple column labels
    grid.SetColLabelValue(0, "Product")
    grid.SetColLabelValue(1, "Quantity")
    grid.SetColLabelValue(2, "Price")
    grid.SetColFormatFloat(2)

    grid.SetCellValue(24, 0, 'Total')

if __name__ == "__main__":
    app = wx.App()
    frame = MyForm().Show()
    app.MainLoop()

can anyone help?


Solution

  • Once you start wanting to do things like that then it is a good idea to use a custom grid table for containing your data values. In other words, instead of you putting your values into the grid and getting the values back out when needed, the grid simply asks your table for the values to display, on an as-needed basis. This makes it easy to deal with the data in whatever structure and data types makes sense for your application. There are some examples in the wxPython demo that show some ways to implement table classes derived from GridTableBase and how to use them.