wxPython检查c++部分是否被删除

wxPython check if the c++ part is deleted

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

我在 wx you can check if the c++ object still exsists by running if self: 中读到过,但这在 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 的单元测试之一时,这证实了我的怀疑。 documentation of 3.0.3 说这应该存在。

如何测试 wx 对象的 c++ 部分是否已在 wxPython 中删除?

事实证明,与 wx 2.8 相比,在调用 Destroy() 或类似的东西之后,您首先必须 wx.Yield()。然后事件得到处理和销毁。

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