wxpython使用grid做电子表格显示数据的时候再网上有很多的例子。但是很多是测试不能通过,总是报错。自己亲自做了一个并且通过测试。高手飘过,如有错误请联系。我运行的环境是python3.4版本,wxpython Phoenix最新的版本。上代码。
先把下面的这个代码保存为
sizers_data_001.py
#-*- encoding:UTF-8 -*-
import wx
import wx.grid
import time
class StudentInfoGridTable(wx.grid.GridTableBase):
def __init__(self):
wx.grid.GridTableBase.__init__(self)
#self.datas = datas
for i in range(3):
i=i+1
#time.sleep( 1 )
print (i)
daii=str(i)
self.data = { (1,1) : "Here",
(1,2) : "is",
(3,3) : "some",
(4,4) : "data",
(1,2) : "Here",
(2,3) : "is",
(4,3) : "some",
(3,4) : daii,
}
self.odd = wx.grid.GridCellAttr()
self.odd.SetReadOnly(True)
self.odd.SetBackgroundColour('yellow')
self.even = wx.grid.GridCellAttr()
self.even.SetReadOnly(True)
pass
def GetNumberRows(self):
return 10
#return len(self.datas)
def GetNumberCols(self):
return 6
#return len(self.colLabels)
def IsEmptyCell(self, row, col):
return self.data.get((row, col)) is not None
def SetValue(self, row, col, value):#给表赋值
self.data[(row,col)] = value
# the table can also provide the attribute for each cell
def GetValue(self, row, col):
value = self.data.get((row, col))
if value is not None:
return value
else:
return ''
#return self.datas[row][col]
pass
def GetAttr(self, row, col, kind):
attr = [self.even, self.odd][row % 2]
attr.IncRef()
return attr
这段代码可以自由保存。但是有一点就是要把这两个文件保存在一个文件夹内。
#-*- encoding:UTF-8 -*-
import wx
import wx.grid
from sizers_data_001 import StudentInfoGridTable
class TestFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="Simple Grid",
size=(640,480))
grid = wx.grid.Grid(self)
table = StudentInfoGridTable()
#这个表是从别的地方创建的类。
grid.SetTable(table, True)
#使用settable的方式来创建表格类。
if __name__ == '__main__':
app = wx.App()
frame = TestFrame()
frame.Show()
app.MainLoop()