I'm trying to make multithreaded pygame app. This is example of the code
import threading
import numpy as np
import time
import sys
import pygame
class Test:
def __init__(self):
pygame.init()
self.surface = pygame.display.set_mode((400, 300))
def do_smth(self):
while True:
time.sleep(2)
print(np.random.randint(10, 20))
def test(self):
p = 10
while True:
self.surface.fill((255, 255, 255))
pygame.draw.rect(self.surface, (255, 0, 0), (p, 10, 70, 65))
event = pygame.event.poll()
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
p += 10
pygame.display.update()
It works if I test it like
T = Test()
T.test()
But when I try to do this using threads - I get error t = threading.Thread(target=T.test) t2 = threading.Thread(target=T.do_smth)
t.start()
t2.start()
t.join()
t2.join()
Exception in thread Thread-1: Traceback (most recent call last): File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner self.run() File "/usr/lib/python3.8/threading.py", line 870, in run self._target(*self._args, **self._kwargs) File "<filename.py>", line 34, in test pygame.display.update() pygame.error: Unable to make GL context current
How can I deal with it?