由于 TreeViewiter 父级,使用生成器进行线程和回调将元素添加到 TreeView 时,pygtk 断言失败

发布于 2024-10-05 11:13:22 字数 1603 浏览 9 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神也荒唐 2024-10-12 11:13:22

只是记得,当我浏览 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文