Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added example on how Window behaves with WINDOWCLOSE and QUIT events #3115

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions docs/reST/ref/window.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,38 @@
:param bool always_on_top: Create a window that is always presented above
others.

Event behavior if one Window is created: When the close button is pressed, the ``QUIT`` event will be sent to the event queue.
.. code-block:: python

import pygame

window = pygame.Window()

while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
raise SystemExit

Event behavior if multiple Windows are created: When the close button is pressed, a ``WINDOWCLOSE`` event is sent. You need to explicitly destroy the window. Note that the event ``QUIT`` will only be sent if all Window has been destroyed.
.. code-block:: python

import pygame

window1 = pygame.Window(position=(0,100))
window2 = pygame.Window(position=(700,100))

while True:
for event in pygame.event.get():
if event.type == pygame.WINDOWCLOSE:
print(f"WINDOWCLOSE event sent to Window #{event.window.id}.")
event.window.destroy()

if event.type == pygame.QUIT:
print(f"Last window is destroyed. QUIT event was sent.")
pygame.quit()
raise SystemExit

.. versionadded:: 2.4.0
.. versionchanged:: 2.5.0 when ``opengl`` is ``True``, the ``Window`` has an OpenGL context created by pygame
.. versionchanged:: 2.5.1 Window is now a base class, allowing subclassing
Expand Down