Skip to content

Commit

Permalink
Make window subclassable
Browse files Browse the repository at this point in the history
  • Loading branch information
zoldalma999 committed Jun 28, 2024
1 parent 497436e commit 31697bb
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion buildconfig/stubs/pygame/window.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ from pygame.rect import Rect
from pygame.surface import Surface

def get_grabbed_window() -> Optional[Window]: ...
@final

class Window:
def __init__(
self,
Expand Down
1 change: 1 addition & 0 deletions docs/reST/ref/window.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@

.. 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


.. attribute:: grab_mouse
Expand Down
1 change: 1 addition & 0 deletions src_c/window.c
Original file line number Diff line number Diff line change
Expand Up @@ -1141,6 +1141,7 @@ static PyTypeObject pgWindow_Type = {
PyVarObject_HEAD_INIT(NULL, 0).tp_name = "pygame.window.Window",
.tp_basicsize = sizeof(pgWindowObject),
.tp_dealloc = (destructor)window_dealloc,
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
.tp_doc = DOC_WINDOW,
.tp_methods = window_methods,
.tp_init = (initproc)window_init,
Expand Down
18 changes: 18 additions & 0 deletions test/window_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,24 @@ def test_window_opengl(self):
pygame.display.quit()
pygame.init()

def test_window_subclassable(self):
class WindowSubclass(Window):
def __init__(self, title="Different title", size=(640, 480), **flags):
super().__init__(title, size, pygame.WINDOWPOS_CENTERED, **flags)
self.attribute = 10

window = WindowSubclass()
self.assertTrue(issubclass(WindowSubclass, Window))
self.assertIsInstance(window, WindowSubclass)
self.assertEqual(window.title, "Different title")
self.assertEqual(window.attribute, 10)
window.destroy()

pygame.display.set_mode((200, 200))
window = WindowSubclass.from_display_module()
self.assertIsInstance(window, WindowSubclass)
self.assertEqual(window.size, (200, 200))

def tearDown(self):
self.win.destroy()

Expand Down

0 comments on commit 31697bb

Please sign in to comment.