Skip to content

Commit bef0d2e

Browse files
authored
Merge pull request #2950 from zoldalma999/window-subclass
Make window subclassable
2 parents 7d9c288 + 31697bb commit bef0d2e

File tree

4 files changed

+21
-1
lines changed

4 files changed

+21
-1
lines changed

buildconfig/stubs/pygame/window.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ from pygame.rect import Rect
66
from pygame.surface import Surface
77

88
def get_grabbed_window() -> Optional[Window]: ...
9-
@final
9+
1010
class Window:
1111
def __init__(
1212
self,

docs/reST/ref/window.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646

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

5051

5152
.. attribute:: grab_mouse

src_c/window.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,6 +1141,7 @@ static PyTypeObject pgWindow_Type = {
11411141
PyVarObject_HEAD_INIT(NULL, 0).tp_name = "pygame.window.Window",
11421142
.tp_basicsize = sizeof(pgWindowObject),
11431143
.tp_dealloc = (destructor)window_dealloc,
1144+
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
11441145
.tp_doc = DOC_WINDOW,
11451146
.tp_methods = window_methods,
11461147
.tp_init = (initproc)window_init,

test/window_test.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,24 @@ def test_window_opengl(self):
397397
pygame.display.quit()
398398
pygame.init()
399399

400+
def test_window_subclassable(self):
401+
class WindowSubclass(Window):
402+
def __init__(self, title="Different title", size=(640, 480), **flags):
403+
super().__init__(title, size, pygame.WINDOWPOS_CENTERED, **flags)
404+
self.attribute = 10
405+
406+
window = WindowSubclass()
407+
self.assertTrue(issubclass(WindowSubclass, Window))
408+
self.assertIsInstance(window, WindowSubclass)
409+
self.assertEqual(window.title, "Different title")
410+
self.assertEqual(window.attribute, 10)
411+
window.destroy()
412+
413+
pygame.display.set_mode((200, 200))
414+
window = WindowSubclass.from_display_module()
415+
self.assertIsInstance(window, WindowSubclass)
416+
self.assertEqual(window.size, (200, 200))
417+
400418
def tearDown(self):
401419
self.win.destroy()
402420

0 commit comments

Comments
 (0)