Since wxWidgets is a C++ library and wxPython is a fairly thin wrapper around that, we have to be a little more careful and use more “defensive programming” than we normally would with pure Python code.
If you look at the crash report that macOS gives you then you can find the C++ stack trace that led to the crash. In this case it has text that looks like something like this:
Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0 libwx_osx_cocoau_core-3.1.4.0.0.dylib 0x0000000108fcd880 wxBitmapRefData::GetImage() const + 16 (bitmap.cpp:354)
1 libwx_osx_cocoau_core-3.1.4.0.0.dylib 0x0000000108fce5fd wxBitmap::GetImage() const + 29 (bitmap.cpp:930)
2 libwx_osx_cocoau_core-3.1.4.0.0.dylib 0x00000001090c9745 wxBitmap::GetNSImage() const + 21 (bitmap.h:206)
3 libwx_osx_cocoau_core-3.1.4.0.0.dylib 0x0000000109102074 wxToolBarTool::UpdateImages() + 52 (toolbar.mm:600)
4 libwx_osx_cocoau_core-3.1.4.0.0.dylib 0x000000010910704b wxToolBar::DoInsertTool(unsigned long, wxToolBarToolBase*) + 2603 (toolbar.mm:1542)
5 libwx_osx_cocoau_core-3.1.4.0.0.dylib 0x00000001092b18aa wxToolBarBase::InsertTool(unsigned long, wxToolBarToolBase*) + 202 (tbarbase.cpp:204)
6 libwx_osx_cocoau_core-3.1.4.0.0.dylib 0x00000001092b1744 wxToolBarBase::DoInsertNewTool(unsigned long, wxToolBarToolBase*) + 52 (tbarbase.h:671)
7 libwx_osx_cocoau_core-3.1.4.0.0.dylib 0x00000001092b16f4 wxToolBarBase::InsertTool(unsigned long, int, wxString const&, wxBitmap const&, wxBitmap const&, wxItemKind, wxString const&, wxString const&, wxObject*) + 324 (tbarbase.cpp:189)
8 libwx_osx_cocoau_core-3.1.4.0.0.dylib 0x00000001092b159d wxToolBarBase::DoAddTool(int, wxString const&, wxBitmap const&, wxBitmap const&, wxItemKind, wxString const&, wxString const&, wxObject*, int, int) + 189 (tbarbase.cpp:172)
9 _core.cpython-37m-darwin.so 0x0000000108420918 wxToolBarBase::AddTool(int, wxString const&, wxBitmap const&, wxBitmap const&, wxItemKind, wxString const&, wxString const&, wxObject*) + 152 (tbarbase.h:300)
10 _core.cpython-37m-darwin.so 0x0000000108420a7e wxToolBarBase::AddTool(int, wxString const&, wxBitmap const&, wxString const&, wxItemKind) + 190 (tbarbase.h:311)
11 _core.cpython-37m-darwin.so 0x0000000108417768 meth_wxToolBar_AddTool(_object*, _object*, _object*) + 792 (sip_corewxToolBar.cpp:1004)
At the bottom we can see that it starts with a call to the toolbar’s AddTool
method, and that it continues on to code that is trying to convert the bitmap to an image. If you then look back at the Python code you’ll find this:
self.toolbar.AddTool(1, '', wx.Bitmap('texit.png'))
But you likely do not have a texit.png
file in your current working directory. (You can probably download it from somewhere on ZetCode.) A more defensive version of the code could be implemented like this:
bmp = wx.Bitmap('texit.png')
assert bmp.IsOk()
self.toolbar.AddTool(1, '', bmp)