Skip to content

Commit f554e77

Browse files
authored
Merge pull request #3115 from MrValdez/window-api
added example on how Window behaves with WINDOWCLOSE and QUIT events
2 parents ded57ba + f6998d4 commit f554e77

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

docs/reST/ref/window.rst

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,45 @@
4444
:param bool always_on_top: Create a window that is always presented above
4545
others.
4646

47+
Event behavior if one Window is created: When the close button is pressed,
48+
the ``QUIT`` event will be sent to the event queue.
49+
50+
.. code-block:: python
51+
52+
import pygame
53+
54+
window = pygame.Window()
55+
56+
while True:
57+
for event in pygame.event.get():
58+
if event.type == pygame.QUIT:
59+
pygame.quit()
60+
raise SystemExit
61+
62+
Event behavior if multiple Windows are created: When the close button is
63+
pressed, a ``WINDOWCLOSE`` event is sent. You need to explicitly destroy
64+
the window. Note that the event ``QUIT`` will only be sent if all Window
65+
has been destroyed.
66+
67+
.. code-block:: python
68+
69+
import pygame
70+
71+
window1 = pygame.Window(position=(0,100))
72+
window2 = pygame.Window(position=(700,100))
73+
74+
while True:
75+
for event in pygame.event.get():
76+
if event.type == pygame.WINDOWCLOSE:
77+
id = event.window.id
78+
print(f"WINDOWCLOSE event sent to Window #{id}.")
79+
event.window.destroy()
80+
81+
if event.type == pygame.QUIT:
82+
print(f"Last window is destroyed. QUIT event was sent.")
83+
pygame.quit()
84+
raise SystemExit
85+
4786
.. versionadded:: 2.4.0
4887
.. versionchanged:: 2.5.0 when ``opengl`` is ``True``, the ``Window`` has an OpenGL context created by pygame
4988
.. versionchanged:: 2.5.1 Window is now a base class, allowing subclassing

0 commit comments

Comments
 (0)