File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 77from typing import TypeVar
88
99__all__ = [
10+ "NoArcadeWindowError" ,
1011 "OutsideRangeError" ,
1112 "IntOutsideRangeError" ,
1213 "FloatOutsideRangeError" ,
2324_CT = TypeVar ("_CT" ) # Comparable type, ie supports the <= operator
2425
2526
27+
28+ class NoArcadeWindowError (RuntimeError ):
29+ """No valid Arcade window exists.
30+
31+ It may be handled as a :py:class:`RuntimeError`.
32+ """
33+ ...
34+
35+
2636class OutsideRangeError (ValueError ):
2737 """
2838 Raised when a value is outside and expected range
Original file line number Diff line number Diff line change 1515import pyglet
1616
1717from arcade .types import RGBA255 , Color
18+ from arcade .exceptions import NoArcadeWindowError
1819
1920if TYPE_CHECKING :
2021 from arcade import Window
2526__all__ = [
2627 "get_display_size" ,
2728 "get_window" ,
29+ "have_window" ,
2830 "set_window" ,
2931 "close_window" ,
3032 "run" ,
@@ -54,14 +56,31 @@ def get_display_size(screen_id: int = 0) -> tuple[int, int]:
5456 return screen .width , screen .height
5557
5658
57- def get_window () -> Window :
59+ def have_window () -> bool :
60+ """Returns ``True`` if an Arcade window exists.
61+
62+ .. tip:: Use this to avoid an :py:class:`~arcade.exceptions.NoArcadeWindowError`.
63+
64+
65+ Returns:
66+ Whether a :py:class:`~arcade.Window` exists.
5867 """
59- Return a handle to the current window.
68+ return _window is None
6069
61- :return: Handle to the current window.
70+
71+ def get_window () -> Window :
72+ """Return a handle to the current window.
73+
74+ If no window exists, it will raise an exception you can
75+ handle as a :py:class:`RuntimeError`. Use :py:func:`have_window`
76+ to prevent raising an exception.
77+
78+ Raises:
79+ :py:class:`~arcade.exceptions.NoArcadeWindowError` when no window exists.
6280 """
81+ # This avoids calling the function above because it may be a hot code path.
6382 if _window is None :
64- raise RuntimeError ("No window is active. It has not been created yet, or it was closed." )
83+ raise NoArcadeWindowError ("No window is active. It has not been created yet, or it was closed." )
6584
6685 return _window
6786
You can’t perform that action at this time.
0 commit comments