pygame-ce icon indicating copy to clipboard operation
pygame-ce copied to clipboard

Pygame window not closing in jupyter notebook (window hangs)

Open pseudo-rnd-thoughts opened this issue 2 years ago • 7 comments
trafficstars

Environment:

  • Operating system (e.g. Windows, Linux(Debian), Linux(Ubuntu), Mac): Mac
  • Python version (e.g. 3.7.9, 3.8.5) : 3.9
  • SDL version (e.g. SDL 2.0.12): 2.26.4
  • PyGame version (e.g. 2.0.0.dev10, 1.9.6): 2.4.0 (and 2.1.3)
  • Relevant hardware (e.g. if reporting a bug about a controller, tell us the brand & name of it):

Current behavior:

I'm part of the development team of OpenAI Gym and Gymnasium and would like users to be able to use pygame in jupyter notebook. We are able to open a window in jupyter notebook however pygame.quit doesn't cause the window to close and the kernel will hang and close

Expected behavior:

To be able to close a pygame windows in jupyter notebook

Steps to reproduce:

pip install gymnasium[classic_control]

import gymnasium as gym

env = gym.make("LunarLander-v2", render_mode="human")
observation, info = env.reset()

for _ in range(100):
   action = env.action_space.sample()
   observation, reward, terminated, truncated, info = env.step(action)

   if terminated or truncated:
      observation, info = env.reset()
env.close()  # This includes `pygame.quit()` 

I have tested this is issue with pygame using the following basic script

import pygame

win = pygame.display.set_mode((500,500))
pygame.display.set_caption("First Game")

run = True
while run:
    pygame.time.delay(100)

    # handle events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    # clear the disaply
    win.fill(0)

    # draw the scene
    filled_rect = pygame.Rect(100, 100, 25, 25)
    pygame.draw.rect(win, (0,0,255), filled_rect)

    # update the dispaly
    pygame.display.update()

pygame.quit()

pseudo-rnd-thoughts avatar Jun 16 '23 10:06 pseudo-rnd-thoughts