0

Using the Generator class from http://unpythonic.blogspot.com/2007/08/using-threads-in-pygtk.html, the following code makes my program fail:

import gtk
import gobject
from GeneratorTask import GeneratorTask

GeneratorTask(self.get_playlist, self.load_playlist).start(playlist_id, model, iter, child)

def get_playlist(self, playlistId=None, treeModel=None, treeIter=None):
    if playlistId is None:
        yield (ten.getRootPlaylist(depth=1), treeModel, treeIter)
    else:
        yield (ten.getPlaylist(playlistId, depth=1), treeModel, treeIter)

def load_playlist(self, playlist, treeModel=None, treeIter=None):
    if treeModel == None:
        treeModel = self.programme

    # Setting treeIter = None prevents assertion fail but 
    # obviously doesn't append the items under the parent
    if len(playlist.find('childPlaylists')):
        for childPlaylist in playlist.find('childPlaylists').findall('playlist'):
            series_iter = treeModel.append(treeIter, [formatTitle(childPlaylist.find('title').text), childPlaylist.find('id').text, True, childPlaylist])
            treeModel.append(series_iter, ['Loading...', None, None, None]) 
    elif len(playlist.find('mediaList')):
        for media in playlist.find('mediaList').findall('media'):
            treeModel.append(treeIter, [media.find('title').text, media.find('id').text, False, media])

The problem seems to be passing the parent ("treeIter" in the code) looses proper reference and is no longer valid by the time the callback is called.

How can I do this properly to ensure new nodes are added under the correct parent, while still maintaining some sort of event-threading (as the ten.getPlaylist functions will block, some threading is needed)?

gpoo
  • 8,408
  • 3
  • 38
  • 53
Adam M-W
  • 3,509
  • 9
  • 49
  • 69

1 Answers1

1

Just remembered, that when I was going through lablgtk tutorial, there was a hint that Gtk.tree_iters are short-lived and there was a suggestion to use Gtk.row_references instead. Sorry, this is not python, however You may find it useful: lablgtk:treeview:referring to rows

barti_ddu
  • 10,179
  • 1
  • 45
  • 53
  • Same thing goes in Python, TreeIters are indeed short-lived. – ptomato Dec 04 '10 at 01:44
  • It seems that TreeIters are short-lived, but I can't seem to find the get_row_reference function in PyGTK or how to convert it back to a TreeIter when I need it for using as a parent in an append function. However, I did find model.get_string_from_iter() and model.get_iter_from_string() which does the job, except I just hope it doesn't loose proper reference because the tree changes before the code is executed. But it seems ok for what I need. – Adam M-W Dec 05 '10 at 08:40