wxpython: Updating a dict or other appropriate data type from wx.lib.sheet.CSheet object

If I have a notebook with three spreadsheet widgets, what is the best way to have changes to the spreadsheet update a dictionary (or maybe an sqlite file?). Do all wx grid objects come with a built in dictionary related to the SetNumberRows and SetNumberCols? Basically I am looking for guidance on how to work with the user-input data from a spreadsheet widget, as in this example adapted from the tutorial on python.org:

class ExSheet(wx.lib.sheet.CSheet):
    def __init__(self, parent):
        sheet.CSheet.__init__(self, parent)
        self.SetLabelBackgroundColour('#CCFF66')
        self.SetNumberRows(50)
        self.SetNumberCols(50)

class Notebook(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title)
        nb = wx.Notebook(self, -1, style=wx.NB_BOTTOM)
        self.sheet1 = ExSheet(nb)
        self.sheet2 = ExSheet(nb)
        self.sheet3 = ExSheet(nb)
        nb.AddPage(self.sheet1, "Sheet1")
        nb.AddPage(self.sheet2, "Sheet2")
        nb.AddPage(self.sheet3, "Sheet3")
        self.sheet1.SetFocus()
        self.StatusBar()


Asked by: Lydia233 | Posted: 05-10-2021






Answer 1

Use a wxGrid with a wxGridTableBase instead

Here is a simple example:

import wx, wx.grid

class GridData(wx.grid.PyGridTableBase):
    _cols = "a b c".split()
    _data = [
        "1 2 3".split(),
        "4 5 6".split(),
        "7 8 9".split()
    ]

    def GetColLabelValue(self, col):
        return self._cols[col]

    def GetNumberRows(self):
        return len(self._data)

    def GetNumberCols(self):
        return len(self._cols)

    def GetValue(self, row, col):
        return self._data[row][col]

    def SetValue(self, row, col, val):
        self._data[row][col] = val

class Test(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None)
        self.data = GridData()
        grid = wx.grid.Grid(self)
        grid.SetTable(self.data)
        self.Bind(wx.EVT_CLOSE, self.OnClose)
        self.Show()

    def OnClose(self, event):
        print self.data._data
        event.Skip()

app = wx.PySimpleApp()
app.TopWindow = Test()
app.MainLoop()

Answered by: Ada690 | Posted: 06-11-2021



Similar questions





Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top