[英]wxPython check if the c++ part is deleted
我正在处理一个项目,其中 function 由于任务延迟而抛出wx.pyDeadObject Error
。
我读过在 wx 中你可以通过运行if self:
来检查 c++ object 是否仍然存在,但是这在 wxPython 3.0.2 中不再有效。 使用 wx 3。
我已将 function 修改为以下内容:
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(我也尝试在 function 的开头复制该 if 语句,但那没有用)。
在运行其中一个单元测试时,我得到了Self existed: True and self.list existed: True
这证实了我的怀疑。 3.0.3 的文档说这应该存在。
你如何测试 wx object 的 c++ 部分是否已在 wxPython 中删除?
事实证明,与wx 2.8相比,您首先必须在调用Destroy()
或类似方法之后使用wx.Yield()
。 然后处理事件并销毁它们。
在所有情况下,根据对象是否仍处于活动状态,使用bool(object)
计算结果为True
或False
。
要知道 wxPython window 的 c++ object 是否仍然存在,您必须使用
self.__nonzero__()
更详细的答案在: Defining a wx.Panel destructor in wxpython
以及来自 wxPython 和 PyDeadObject 错误的文档: https://wxpython.org/Phoenix/docs/html/MigrationGuide.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.