My jupyter notebook kernel often dies from this pygame code

Viewed 749

I wrote a pygame code like this.

pygame.init()
win = pygame.display.set_mode((500, 500))
pygame.display.set_caption("Test")
black = (0, 0, 0)
sysfont = pygame.font.SysFont(None, 50)
win.fill((255, 255, 255))

running = True
while running:
    win.fill((255, 255, 255))
    digit = sysfont.render("Test", 1, black)
    win.blit(digit, (50, 50))
    pygame.display.update()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
            
pygame.quit()

So That's just to create a window and show some text on it. I runned it on Jupyter Notebook and it worked. but when I closed the window and tried to run it again, a window came up but closed of itself and Jupyter said "The kernel appears to have died." This happened again and again. I runned it, it worked. but I rerunned it, it failed. Is there anything wrong with this code?

3 Answers

I think it's an issue with Anaconda, as the same issue has been faced by me in spyder kernel. For spyder it restarts the kernel, which is a little time taking but i don't think there exists a solution for the same.

I hope you're safe in this time of an ongoing pandemic!

I faced the same problem, and I kind of addressed it by stopping the kernel instead of exiting the pygame. when I stop the kernel, the pygame windows also stoped and in the next run, it starts without killing the kernel. Hope it help.

I bypassed this problem by adding %reset -f at the first line before all the import statements. I can run and close the window without the dead kernel prompt. Hopefully this helps people looking for this kind of solution.

Related