Jul-08-2020, 06:49 PM
Dear all,
I am working on a project where pygame is running in a background tread to provide a GUI and the main program is interacting indirectly with pygame. For this project it is absolutely crucial that pygame is not mentioned in the main loop. A rather striped down example would look like this:
This example runs absolutely fine on Linux but crashes on Windows 10. I assume this is caused by the two loops running in parallel, while only one is handling the pygame (Windows) events. Maybe threading is not the right approach here, but as I am not very experienced in multiprocessing and Co I am totally lost. Would someone be able to help? Thanks a lot in advance.
I am working on a project where pygame is running in a background tread to provide a GUI and the main program is interacting indirectly with pygame. For this project it is absolutely crucial that pygame is not mentioned in the main loop. A rather striped down example would look like this:
import pygame import threading import time class cbg_pygame(threading.Thread): """ Main class emulating the GPIO and i2c and draws the main window. """ def __init__(self, xoffset = 150, yoffset = 20): threading.Thread.__init__(self) # Initialise pygame and create screen, surfaces and Gadgets (Input/Buttons/Lists...) pygame.init() # Define some colors self.pos=[0,0] #Position of the ball on the screen self.dimension=[800,600] #screen dimensions self.size=30 #size of the ball self._screen = pygame.display.set_mode((self.dimension[0], self.dimension[1])) pygame.display.set_caption('pygames-test') #load the background image pygame.mouse.set_visible(1) pygame.key.set_repeat(1, 30) def run(self): """ Mainloop of the emulated GPIO. Is called when the emulator class is initialized. """ self.running = True # create clock to reduce framerate clock = pygame.time.Clock() while self.running: clock.tick(30) #reduce framerate to 30 fps # get all events and check them events = pygame.event.get() for event in events: if event.type == pygame.QUIT: self.running = False self._screen.fill((255, 255, 255)) pygame.draw.ellipse(self._screen, (0, 0, 0), [self.pos[0], self.pos[1], self.size, self.size], 4) pygame.display.flip() pygame.quit() def stop(self): self.running = False bgpg = cbg_pygame() bgpg.start() direction=[1,1] while(bgpg.running): #Program is stopped if pygame window is closed for i in range(2): if direction[i]: #direction[0]=x, direction[1]=y, traveling with a velocity of 20 pixels/circle if bgpg.pos[i]+20 < bgpg.dimension[i]-bgpg.size: bgpg.pos[i]=bgpg.pos[i]+20 else: bgpg.pos[i]=bgpg.dimension[i]-bgpg.size direction[i]=0 else: if bgpg.pos[i]-20 > 0: bgpg.pos[i]=bgpg.pos[i]-20 else: bgpg.pos[i]=0 direction[i]=1 time.sleep(0.1)I know this example makes no sense but it demonstrates what I need to achieve. There is a main loop controlling some stuff in a class and pygame is running in the background to display the data in some form.
This example runs absolutely fine on Linux but crashes on Windows 10. I assume this is caused by the two loops running in parallel, while only one is handling the pygame (Windows) events. Maybe threading is not the right approach here, but as I am not very experienced in multiprocessing and Co I am totally lost. Would someone be able to help? Thanks a lot in advance.