I have a python program listening to windows messages in a loop. Getting some live data from this loop.
Loop syntax https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getmessagew
BOOL bRet;
while( (bRet = GetMessage( &msg, hWnd, 0, 0 )) != 0)
{
if (bRet == -1)
{
// handle the error and possibly exit
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
Now when i want to show this data i use this code block to refresh plot in a callback
def callback(value,item):
x.append(value)
y.append(datetime.now())
plt.clf()
plt.plot(y,x)
plt.draw()
plt.pause(0.0001)
Every time i move mouse over plot(or do anything about the window actually) it triggers my loop code. Also resize gives this error.
Fatal Python error: PyEval_RestoreThread: NULL tstate Python runtime state: initialized
How can i stop this? I tried to use threads but failed to share data to the thread to show the plot. Not even sure if this will help
Edit: giving plt.pause() a bigger number then callbacks intervals does exactly what i want but if number is very big like "4294967" then sleeps stacks and crash program very quickly. If number is small like 5 and callback is not called in that time windows starts registering mouse which leads to a crash with same error if i move mouse over window.
Edit2: Using matplotlib.use("Qt5agg") removes most of my problems and resize work.But still fires mouse events and some more everytime callback called. Also since this backend prevents plot from stealing focus but makes it flashing every half seconds which also triggers windows message loop.
Edit3:Opening figures options(or any other button of plot) does what i want. How can i make sure main window of plot also does the same?