Why does my GtkTreeView sort func receive a row with None in it?
I've set up a gtk.TreeView
with a gtk.TreeStore
. One column contains formatted dollar amounts, and I've set up sorting by that column as follows:
def sortmon(model, i1, i2):
v1 = model[i1][COL_MONEY]
v2 = model[i2][COL_MONEY]
return cmp(flo开发者_如何学Cat(v1.replace("$","").replace(",","")),
float(v2.replace("$","").replace(",","")))
self.hsModel.set_sort_func(COL_MONEY, sortmon)
This works fine, except that sometimes, when I append a row, I get:
stderr : INFO Traceback (most recent call last):
stderr : INFO File "C:\Users\DrClaud\bumhunter\gui\widgets\replay\ReplayWidget.py", line 141, in sortpot
stderr : INFO float(v2.replace("$","").replace(",","")))
stderr : INFO AttributeError: 'NoneType' object has no attribute 'replace'
I did more print outs, and it seems that when I insert a row, one of model[i1][x]
or model[i2][x]
for any x will be None. I'm certain I'm not inserting a row with None
elements in it.. so what happens?
If you append a row to a sorted model, GTK+ automatically searches for a proper position for it and thus your sort function is called if it's on that column. You should either handle None
specially, or specify initial values in append()
call, like:
model.append (parent, [x, y, z])
The latter of course only solves the problem if you can specify something more appropriate than None
, an empty string, perhaps.
精彩评论