wxPython如何检查C++部分是否已删除?

3

我正在处理一个项目,其中由于延迟任务,一个函数会抛出 wx.pyDeadObject Error

我读到在 wx 中 可以通过运行 if self: 来检查 c++ 对象是否仍然存在, 但是在使用 wxPython 3.0.2 和 wx 3 时,这种方法不再奏效。

我已将该函数修改为以下内容:

def SetData(self, delayedResult):
    if not self or not self.list:
        return
    data = []
    torrents = delayedResult.get()

    for torrent in torrents:
        data.append((torrent.infohash, [torrent.name], torrent, ThumbnailListItemNoTorrent))

    self_exist = True
    list_exists = True

    if not self:
        self_exist = False
    if not self.list:
        list_exists = False

    try:
        self.list.SetData(data)
        self.list.SetupScrolling()
    except wx.PyDeadObjectError:
        print "Self existed: %s and self.list existed: %s" % (self_exist, list_exists)

它已经通过了第一个if语句,但由于delayedResutlt.get()是阻塞的,我添加了额外的try except(我还尝试在函数开头重复该if语句,但那行不通)。

运行其中一个单元测试时,我得到了Self existed: True and self.list existed: True,这证实了我的怀疑。 3.0.3的文档说应该存在这个东西。

如何测试wxPython中的c++部分是否已被删除?


我曾经对旧版wx有同样的问题,结果答案也是一样的。我无法写出几乎相同的问题,所以将这个问题概括为适用于旧版wx。 - ntg
2个回答

6

原来,在调用Destroy()或类似函数后,你需要首先调用wx.Yield()。与wx 2.8不同的是,事件会被处理和销毁。

在所有情况下,使用bool(object)将评估为TrueFalse,取决于对象是否仍处于活动状态。


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接