goopylib.window

class Window(width: int, height: int, title: str = 'goopylib Window')

Creates a window that can be used to draw graphics and widgets.

Parameters
  • width (int) – in screen coordinates

  • height (int) – in screen coordinates

  • title (str, default: 'goopylib Window') – displayed in the title bar

is_closed() bool
Returns

bool – whether the window is closed.

is_open() bool
Returns

bool – whether the window is open.

is_destroyed() bool
Returns

bool – whether the window has been destroyed.

update() None

Refreshes the window.

Return type

None

destroy() None

Destroys & closes the window.

Warning

Once the window is destroyed, accessing its attributes or calling its methods is undefined.

Return type

None


Window Attributes

Window instances have many attributes that can be modified to configure the look of the Window. Here is a list:

Window.width

The width of the window.

Window.height

The height of the window.

Window.title

The title of the window.

Window.xpos

The x position of the window.

Window.ypos

The y position of the window.

Window.background

The background color of the window.

Window.min_width

The minimum width of the window.

Window.min_height

The minimum height of the window.

Window.max_width

The maximum width of the window.

Window.max_height

The maximum height of the window.

Window.resizable

Whether the window is resizable by the user or not.

Window.decorated

Whether the window has window decorations.

Window.floating

Whether the window is always on top.

Window.auto_minimized

Whether the window is automatically minimized when it loses focus.

Window.focused_on_show

Whether the window gains input focus when it is shown.

set_size(width: int, height: int) None

Sets the size of the window.

Parameters
  • width (int) – in screen coordinates

  • height (int) – in screen coordinates

Raises
  • TypeError – width and height must be integers

  • ValueError – width and height must be greater than 0

Return type

None

set_position(xpos: int, ypos: int) None

Sets the position of the top-left of the window on the screen.

Parameters
  • xpos (int) – in screen coordinates

  • ypos (int) – in screen coordinates

Raises

TypeError – xpos and ypos must be integers

Return type

None

get_frame_size() tuple[int, int, int, int]

Gets the size, in screen coordinates, of the window frame.

Decorated windows have title bars and window frames around them. You can retrieve the sizes of these using this method. The returned values are the distances, in screen coordinates, from the edges of the content area to the corresponding edges of the full window. As they are distances and not coordinates, they are always zero or positive.

Returns

The size of the window’s frame in order (left, top, right, bottom)

get_content_scale() tuple[float, float]

Gets the scale of the content on the window.

The content scale is the ratio between the current DPI and the platform’s default DPI.

Returns

A tuple representing the content scale as (xscale, yscale)

get_framebuffer_size() tuple[int, int]

The size of the framebuffer for the window.

Returns

A tuple (width, height) representing the dimensions, in pixels, of the framebuffer

Note

These methods only apply to resizable Windows

set_size_limits(min_width: int, min_height: int, max_width: int, max_height: int) None

Sets the minimum and maximum resizable dimensions of the window.

Raises
  • TypeError – All the parameters must be integers or None

  • ValueErrormin_width and min_height must be greater than or equal to 0. max_width and max_height must be greater than or equal min_width and min_height

Return type

None

set_min_size(min_width: int, min_height: int) None

Sets the minimum resizable dimensions of the window.

Raises
  • TypeErrormin_width and min_height must be integers

  • ValueErrormin_width and min_height must be greater than or equal to 0

Return type

None

set_max_size(max_width: int, max_height: int) None

Sets the maximum resizable dimensions of the window.

Raises
  • TypeErrormax_width and max_height must be integers or None

  • ValueErrormax_width and max_height must be greater than or equal to min_width and min_height.

Return type

None


get_aspect_ratio() tuple[int, int]

Gets the current aspect ratio of the window.

Returns

A tuple representing the aspect ratio as (numerator, denominator)

set_aspect_ratio(numerator: int, denominator: int) None

Sets the aspect ratio of the window.

The aspect ratio is specified as a numerator and denominator, corresponding to the width and height, respectively. If you want a window to maintain its current aspect ratio, use its current size as the ratio.

Set both arguments to None to remove any aspect ratio configuration.

Raises
  • TypeError – numerator and denominator must be integers or None

  • ValueError – numerator and denominator must be greater than 0

Return type

None


Window State Methods

restore() None

Restores a minimized, maximized, or fullscreen window back to normal.

Return type

None

fullscreen() None

Makes the window enter fullscreen mode.

Return type

None

is_fullscreen() bool
Returns

bool – whether the window is in fullscreen mode.


minimize() None

Minimizes the window.

Return type

None

is_minimized() bool
Returns

bool – whether the window is in minimized mode.

maximize() None

Maximizes the window.

Return type

None

is_maximized() bool
Returns

bool – whether the window is in maximized mode.


show() None

Makes the window visible to the user.

Return type

None

hide(hide: bool = True) None

Makes the window invisible to the user.

Parameters

hide (bool, default: True) – whether to hide/show the window

Return type

None

is_visible() bool
Returns

bool – whether the window is visible to the user.


focus() None

Gives the window input focus and brings it to the front.

Return type

None

has_focus() bool
Returns

bool – whether the window has input focus.

request_attention() None

Requests for the user’s attention to the window.

The system will highlight the window, or on platforms where this is not supported, the application as a whole. Once the user has given it attention, the system will automatically end the request.

Return type

None


Window Events

is_mouse_hovering() bool
Returns

bool – whether the mouse is hovering over the window.

get_mouse_position() tuple[float, float]
Returns

a tuple (xpos, ypos) with the position of the mouse cursor

set_cursor_mode(mode: str) None
Normal:

makes the cursor visible and behave normally.

Hidden:

hides the cursor when it is over the window.

Disabled:

hides and grabs the cursor, providing virtual and unlimited cursor movement.

Parameters

mode (str) – one of “Normal”, “Disabled”, or “Hidden”

Return type

None

check_shift_key() bool
Returns

bool – whether the shift key is being pressed

check_control_key() bool
Returns

bool – whether the control key is being pressed

check_alt_key() bool
Returns

bool – whether the alt key is being pressed

check_super_key() bool
Returns

bool – whether the super key is being pressed

check_key(keycode: int) bool
Parameters

keycode (int) – an int corresponding to the keycode

Returns

bool – whether the specified key is being pressed

check_mouse_button(button: int) bool
Parameters

button (int) – an int corresponding to the mouse button

Returns

bool – whether the specified mouse button is being pressed

check_left_click() bool
Returns

bool – whether the left mouse button is being pressed

check_middle_click() bool
Returns

bool – whether the middle mouse button is being pressed

check_right_click() bool
Returns

bool – whether the right mouse button is being pressed


Camera & Projection

get_camera() Camera
Returns

Camera – the Camera object associated with the window

to_world(x: float, y: float) tuple[float, float]

Converts coordinates in screen space to world space.

(0, 0) is the upper-left of the window in screen space

(width, height) is the lower-right in screen space

Returns

a tuple (x, y) in world coordinates

to_screen(x: float, y: float) tuple[float, float]

Converts coordinates in world space to screen space.

With the default camera projection, (0, 0) is the center in world space.

Returns

a tuple (x, y) in screen coordinates


Static Methods

static update_all() None

Updates all the windows currently active.

Return type

None

static destroy_all() None

Destroys all active windows.

Return type

None


Callback Functions

The Window class provides several callback functions that can be set to execute code whenever certain events take place:

Window.resize_callback

Callback executed when the window is resized.

Window.close_callback

Callback executed when the window is attempted to be closed.

Window.destroy_callback

Callback executed when the window is destroyed.

Window.position_callback

Callback executed when the window is moved.

Window.minimize_callback

Callback executed when the window is minimized.

Window.maximize_callback

Callback executed when the window is maximized.

Window.focus_callback

Callback executed when the window gains input focus.

Window.refresh_callback

Callback executed when the window needs refreshing.

Window.content_scale_callback

Callback executed when the window's content scale changes.

Window.framebuffer_size_callback

Callback executed when the window's framebuffer size changes.

Window.mouse_motion_callback

Callback executed when the mouse moves.

Window.mouse_enter_callback

Callback executed when the mouse enters/exits the window.

Window.scroll_callback

Callback executed when the mouse scrolls.

Window.left_click_callback

Callback executed when the left mouse button is clicked.

Window.middle_click_callback

Callback executed when the middle mouse button is clicked.

Window.right_click_callback

Callback executed when the right mouse button is clicked.

These can be accessed and set like so:

def callback_function():
    print("Window has been destroyed")

print(window.destroy_callback)  # default value is None
window.destroy_callback = callback_function