与这个问题 wxPython + PyObjC causes app to crash at the end 类似,但是代码中有更多多余的代码。
我有以下两个代码示例,后者崩溃,前者运行良好。
代码中唯一的区别是导入的顺序。
这段代码运行良好。
import time
# note that the line is before this is so the code does work this is the only
# change that seems to matter
# need the following line to be AFTER wx import otherwise runs fine
import objc # or import Foundation or probably any objc library
import wx
# need the following line
app = wx.App(redirect=False)
# sleep shows it is ONLY when the code finally ends not before
time.sleep(3)
# you don't even need the MainLoop call
此代码在程序停止运行时运行段错误。
import time
import wx
# need this line to be AFTER wx import otherwise runs fine
import objc # or import Foundation or probably any objc library
# need the following line
app = wx.App(redirect=False)
# sleep shows it is ONLY when the code finally ends not before
time.sleep(3)
# you don't even need the MainLoop call
如何复制
最佳答案
临时解决方案
注意:这仅适用于特定情况,并且仍然是一种黑客行为,因此不应始终使用。
这似乎有效但令人反感,因为它只修复错误而不是下划线代码问题
确保在导入 wx 或 之前导入 objc 任何库都执行 (例如,在我使用扭曲网络库之前我也必须这样做,因为我安装了一个与 wx 一起使用的 react 器(因此它导入了 wx) )。