由于 TreeViewiter 父级,使用生成器进行线程和回调将元素添加到 TreeView 时,pygtk 断言失败
使用 http://unpythonic.blogspot.com/ 中的 Generator 类2007/08/using-threads-in-pygtk.html,以下代码使我的程序失败:
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])
问题似乎是传递父级(代码中的“treeIter”)失去了正确的引用并且不再有效当回调被调用时。
如何正确执行此操作,以确保在正确的父节点下添加新节点,同时仍保持某种事件线程(由于 ten.getPlaylist
函数将阻塞,因此需要一些线程)?
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)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只是记得,当我浏览 lablgtk 教程时,有人暗示 Gtk.tree_iters 是短暂的,并且有人建议使用 Gtk.row_references 代替。抱歉,这不是 python,但是您可能会发现它很有用: lablgtk:treeview:引用行
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