Pygame is not threadsafe and the eventloop is required to run on the main thread! Otherwise, the problem you describe can occur.
One solution is to call pygame.mainloop()
from the main thread.
However,maybe you are using other modules that also require running from the main thread. There is in this case one pythonic solution. you have the possibility to run pygame mainloop with an argument. This argument means: run the mainloop for only a few seconds. Hence what you can do is create a generator that runs mainloop for a 0.1 second that you call periodically from main thread. For example:
def continue_pygame_loop():
pygame.mainloop(0.1)
yield
then just call continue_pygame_loop()
periodically from main thread
Tkinter suffers from the same problem, but has no way to specify runloop()
with a timeout. For me, this is why pygame is great!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…