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()。但是,這會阻止直接訪問表格。
總結
以上是生活随笔為你收集整理的wxpython下载缓慢_我可以在wxPython的wx.grid.Grid中加速优化GridCellAttr的使用吗?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 不等式约束的拉格朗日乘数法_Abaqus
- 下一篇: php 随机在文章中添加锚文本_SEO站