Just a quick example on how to use the wx.grid.PyGridTableBase with wxPython's wx.grid.Grid. Provides a simple way to create, load and use a spreadsheet like grid:
# exploring wxPython's wx.grid.Grid()
# load grid via a '(row, col): value' dictionary and a table class
# that inherits and applies wx.grid.PyGridTableBase
# snee
import wx
import wx.grid
class MyFrame(wx.Frame):
def __init__(self, header_list, data_dict):
wx.Frame.__init__(self, None, wx.ID_ANY,
title="ACME Inc. Staff Stats", size=(510,200))
self.grid = wx.grid.Grid(self)
self.grid.SetRowLabelSize(30) # sets leading row width
self.grid.SetDefaultRowSize(20) # sets all row height
self.grid.SetColLabelSize(30) # sets leading col height
self.grid.SetDefaultColSize(80) # sets all col width
# select the cell with a mouse left click
self.Bind(wx.grid.EVT_GRID_CELL_LEFT_CLICK, self.cell_click)
# the table takes care of all the grid stuff
table = MyTable(header_list, data_dict)
self.grid.SetTable(table, True)
# optional, calculate rows of data needed for fun stats
self.rows = max(data_dict.keys())[0] + 1
def cell_click(self, event):
"""cell has been left clicked"""
row = event.GetRow()
col = event.GetCol()
# move the grid's cursor to frame the cell
self.grid.SetGridCursor(row, col)
# you can do some fun stats with the numbers
num = []
if col > 0:
#num = []
for nrow in range(self.rows):
num.append(float(self.grid.GetCellValue(nrow, col)))
val = self.grid.GetCellValue(row, col)
if num:
sf = "r%dc%d %s max=%.1f min=%.1f avg=%.1f" % \
(row, col, val, max(num), min(num), sum(num)/nrow)
else:
sf = "r%dc%d %s" % (row, col, val)
self.SetTitle(sf)
class MyTable(wx.grid.PyGridTableBase):
def __init__(self, header_list, data_dict):
wx.grid.PyGridTableBase.__init__(self)
self.data = data_dict
# calculate rows and cols needed to fit the data
self.rows = max(data_dict.keys())[0] + 1
self.cols = max(data_dict.keys())[1] + …