0

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(float(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?

gpoo
  • 8,408
  • 3
  • 38
  • 53
Claudiu
  • 224,032
  • 165
  • 485
  • 680

1 Answers1

1

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.

  • i am specifying initial values.. how else would you append a row? `model.append(parent)`? – Claudiu Oct 03 '10 at 22:55
  • 1
    @Claudiu: I checked PyGTK source code, internally it first appends a blank row, then sets the values you passed. This is a bug (or maybe a missing feature) in PyGTK, it should use `gtk_tree_store_insert_with_values()` if `row` is specified. I suggest you file a bug. –  Oct 06 '10 at 18:18
  • oh hah that explains it.. very interesting. thanks for tracking that down! – Claudiu Oct 06 '10 at 18:24