I’m building an application using wxPython 3.0 on Windows, and I’m seeing an exception while trying to change the label of an existing wx.StaticText control. The exception is highly reproducible but unfortunately I haven’t been able to create a test case that displays the same behavior. The exception traceback looks like
Traceback (most recent call last):
[...]
File "...\application\views\markers_panel.py", line 137, in redraw
self.grid_sizer.GetItem(num_cols*(index + 1) + 1).GetWindow().SetLabel(wavelength)
File "C:\Python27\lib\site-packages\wx-3.0-msw\wx\_core.py", line 9210, in SetLabel
return _core_.Window_SetLabel(*args, **kwargs)
PyAssertionError: C++ assertion "m_hdc" failed at ..\..\src\msw\textmeasure.cpp(64)
in wxTextMeasure::BeginMeasuring(): Must not be used with non-native wxDCs
I’m using a wx.FlexGridSizer as a data table by giving it a number of wx.StaticText children. Creating the StaticText objects goes fine, but when I try to change their labels (to update the data shown) I get this exception. The same code works fine under wxPython 2.9.5 in OS X.
I know it’s poor form to submit a crash report without any example code, but can anyone point me in the direction of what might be wrong here?
The problem turned out to be that I was trying to call SetLabel
from one thread while another thread was also modifying the GUI. The solution was to schedule all GUI-updating tasks using wx.CallAfter()
(as described in the accepted answer to this other question). Using this function forces the updating to take place on the main thread, which of course implies that only one updating task can be running at any given time.