0

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?

bdesham
  • 15,430
  • 13
  • 79
  • 123

2 Answers2

3

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.

Community
  • 1
  • 1
bdesham
  • 15,430
  • 13
  • 79
  • 123
1

This is really weird, this assert shouldn't be triggered when using a control. Is it possible that you simply use too many controls and so run out of Windows resources? Have a look at the number of GDI objects (you may need to enable this column explicitly) used by your program in Process Explorer, does it look exceedingly high?

Otherwise I really have no idea about what could be causing this and if you can't reproduce this in a simple example the only way to debug it I see is to try debugging C++ code on your machine.

VZ.
  • 21,740
  • 3
  • 39
  • 42
  • I had exactly the same ```PyAssertionError``` and thanks to your hint i found the issue. I use a wx.grid to display measurements of some sensors in a table over time and after some time the Application crashed. The issue was, that depending on the value I use a certain ```GridCellAttr``` which also includes a Font and Fonts are GDIObjects. So my GID Object count exceeded 10.000 which is my maximum allowed value. I changed my code to reuse the same Font for all ```GridCellAttr``` objects and now everything works fine! – Michael K. Jun 25 '18 at 21:31