wxpython下载缓慢_我可以在wxPython的wx.grid.Grid中加速优化GridCellAttr的使用吗?

设置单元格属性将新GridCellAttr添加到GridCellAttrProvider名单。 随着列表的增长,查找单元格的特定属性(通过遍历列表和比较坐标)变得越来越慢。

您可以尝试通过PyGridTableBase.SetAttr和GetAttr(例如使用字典)实现自己的加快步伐:

编辑:更新后的代码,以允许重写属性和仿效的默认实现属性的所有权。

class MyTable(wx.grid.PyGridTableBase):

atts = {}

def Hash(self,row,col):

#FIXME: assumes a constant number of rows and rows > cols

return col + row * self.GetNumberRows()

def SetAttr(self,attr,row,col):

HASH = self.Hash(row, col)

if HASH in self.atts:

# decrement usage count of existing attr

self.atts[HASH].DecRef()

#assign new attribute

self.atts[HASH] = attr

def GetAttr(self,row,col,kind):

HASH = self.Hash(row, col)

if HASH in self.atts:

attr = self.atts[HASH]

attr.IncRef() # increment reference count

return attr

return None

要允许设置整个行和列,你还必须实现:

def SetRowAttr(self,attr,row):

for col in range(self.GetNumberCols()):

attr.IncRef() # increment reference count for SetAttr

self.SetAttr(attr,row,col)

attr.DecRef() # attr passed to SetRowAttr no longer needed

def SetColAttr(self,attr,col):

for row in range(self.GetNumberRows()):

attr.IncRef()

self.SetAttr(attr,row,col)

attr.DecRef()

注:路过时GridCellAttr到Set*Attr(),默认的实现将采取属性的所有权。 要重新使用相同的属性(例如存储在类变量中),您必须先将其使用Clone()或将其使用计数递增(IncRef()) ,然后再将其传递给Set*Attr()方法(克隆可能会增加内存消耗)。

上述示例没有正确删除属性:SetAttr()可能会检查无attr并在指定的坐标处递减ref计数,然后从dict中删除条目。 SetCol/RowAttr()可以通过添加为ROW和COL,类似于SetAttr() http://stardict.sourceforge.net/Dictionaries.php下载进行优化。 GetAttr()然后可以检查行和冒号中的现有条目,并使用单元格dict中的属性(这是默认实现使用的原则)合并/覆盖该属性。要正确清理字典,请在.clear()之前的每个条目上拨打DecRef。

或者,您可以从wx.grid.GridCellAttrProvider派生并附加PyGridTableBase.SetAttrProvider()。但是,这会阻止直接访问表格。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值