pygtk assertion fail when using a generator to thread and callback to add elements to a treeview due to treeviewiter parent
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)?
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
精彩评论