熊猫数据框到 wxGrid

时间:2021-07-09 06:04:50

标签: pandas dataframe wxpython wxgrid

我正在使用 GridTableBase 类来显示我的数据框。出于某种原因,网格在第一列中显示了我的 Pandas 索引,我不知道为什么。我试过 if col ==0 并没有任何改变。有没有人解释一下?

class DataTable(gridlib.GridTableBase):
    def __init__(self, data):

        gridlib.GridTableBase.__init__(self)
        self.data = data
       
        #Store the row and col length to see if table has changed in size
        self._rows = self.GetNumberRows()
        self._cols = self.GetNumberCols()

        self.odd=gridlib.GridCellAttr()
        self.odd.SetBackgroundColour((217,217,217))
        self.even=gridlib.GridCellAttr()
        self.even.SetBackgroundColour((255,255,255))

        print(print(len(self.data.columns)))
    def GetAttr(self, row, col, kind):
        attr = [self.even, self.odd][row % 2]
        attr.IncRef()
        return attr

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

    def GetNumberCols(self):
        return len(self.data.columns) + 1

    def IsEmptyCell(self, row, col):
        return False

    def GetValue(self, row, col):
        if col == 0:
            pass
        #    return None #self.data.index[row]
        else:
            return self.data.iloc[row, col-1]

    def SetValue(self, row, col, value):
        if col == 0:
            pass
        else:
            self.data.iloc[row, col - 1] = value
    
    def GetColLabelValue(self, col):
        try:
            if col == 0:
                return ""
                #return 'Index' if self.data.index.name is None else self.data.index.name
            else:
                return self.data.columns[col - 1] #[col-1]
        except:
            pass
       
#---------------------------------------------------------------------------
class DataGrid(gridlib.Grid):
    def __init__(self, parent, data): # data
        gridlib.Grid.__init__(self, parent, - 1) #,colnames,-1 # data

        self.table = DataTable(data)
      
        # The second parameter means that the grid is to take ownership of the
        # table and will destroy it when done.  Otherwise you would need to keep
        # a reference to it and call it's Destroy method later.
        self.SetTable(self.table, True)
        self.Bind(gridlib.EVT_GRID_CELL_RIGHT_CLICK, self.OnCellRightClick)
        #self.data_grid.Bind(wx.grid.EVT_GRID_CELL_CHANGED, self.onCellChanged)
        #self.data_grid.Bind(wx.grid.EVT_GRID_CELL_RIGHT_DCLICK,self.onDeleteRecord)
        #self.data_grid.Bind(gridlib.EVT_GRID_CELL_RIGHT_CLICK,self.showPopupMenu)
        #self.data_grid.CreateGrid(rows,cols) #self.data_grid.CreateGrid(219, 16)

我尝试删除它:

self.grid.DeleteCols(0,1)

但我也做不到。它抛出异常。

"C++ assertion ""Assert failure"" failed at ....\src\generic\grid.cpp(1471) in wxGridTableBase::DeleteCols(): Called grid table class function DeleteCols 但您的派生表类不会覆盖此函数"

enter image description here

0 个答案:

没有答案