wxpython下载缓慢_我可以在wxPython的wx.grid.Grid中加速优化GridCellAttr的使用吗?
設(shè)置單元格屬性將新GridCellAttr添加到GridCellAttrProvider名單。 隨著列表的增長,查找單元格的特定屬性(通過遍歷列表和比較坐標(biāo))變得越來越慢。
您可以嘗試通過PyGridTableBase.SetAttr和GetAttr(例如使用字典)實(shí)現(xiàn)自己的加快步伐:
編輯:更新后的代碼,以允許重寫屬性和仿效的默認(rèn)實(shí)現(xiàn)屬性的所有權(quán)。
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
要允許設(shè)置整個(gè)行和列,你還必須實(shí)現(xiàn):
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()
注:路過時(shí)GridCellAttr到Set*Attr(),默認(rèn)的實(shí)現(xiàn)將采取屬性的所有權(quán)。 要重新使用相同的屬性(例如存儲在類變量中),您必須先將其使用Clone()或?qū)⑵涫褂糜?jì)數(shù)遞增(IncRef()) ,然后再將其傳遞給Set*Attr()方法(克隆可能會增加內(nèi)存消耗)。
上述示例沒有正確刪除屬性:SetAttr()可能會檢查無attr并在指定的坐標(biāo)處遞減ref計(jì)數(shù),然后從dict中刪除條目。 SetCol/RowAttr()可以通過添加為ROW和COL,類似于SetAttr() http://stardict.sourceforge.net/Dictionaries.php下載進(jìn)行優(yōu)化。 GetAttr()然后可以檢查行和冒號中的現(xiàn)有條目,并使用單元格dict中的屬性(這是默認(rèn)實(shí)現(xiàn)使用的原則)合并/覆蓋該屬性。要正確清理字典,請?jiān)?clear()之前的每個(gè)條目上撥打DecRef。
或者,您可以從wx.grid.GridCellAttrProvider派生并附加PyGridTableBase.SetAttrProvider()。但是,這會阻止直接訪問表格。
總結(jié)
以上是生活随笔為你收集整理的wxpython下载缓慢_我可以在wxPython的wx.grid.Grid中加速优化GridCellAttr的使用吗?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 不等式约束的拉格朗日乘数法_Abaqus
- 下一篇: python list转map_Pyth