pygame.display — pygame-ce v2.5.3 documentation
pygame.display — pygame-ce v2.5.3 documentation
|| Reference Index
pygame.display
pygame module to control the display window and screen
pygame.display.init
Initialize the display module
pygame.display.quit
Uninitialize the display module
pygame.display.get_init
Returns True if the display module has been initialized
pygame.display.set_mode
Initialize a window or screen for display
pygame.display.get_surface
Get a reference to the currently set display surface
pygame.display.flip
Update the full display Surface to the screen
pygame.display.update
Update all, or a portion, of the display. For non-OpenGL displays.
pygame.display.get_driver
Get the name of the pygame display backend
pygame.display.Info
Create a video display information object
pygame.display.get_wm_info
Get information about the current windowing system
pygame.display.get_desktop_sizes
Get sizes of active desktops
pygame.display.list_modes
Get list of available fullscreen modes
pygame.display.mode_ok
Pick the best color depth for a display mode
pygame.display.gl_get_attribute
Get the value for an OpenGL flag for the current display
pygame.display.gl_set_attribute
Request an OpenGL display attribute for the display mode
pygame.display.get_active
Returns True when the display is active on the screen
pygame.display.iconify
Iconify the display surface
pygame.display.toggle_fullscreen
Switch between fullscreen and windowed displays
pygame.display.set_gamma
Change the hardware gamma ramps
pygame.display.set_gamma_ramp
Change the hardware gamma ramps with a custom lookup
pygame.display.set_icon
Change the system image for the display window
pygame.display.set_caption
Set the current window caption
pygame.display.get_caption
Get the current window caption
pygame.display.set_palette
Set the display color palette for indexed displays
pygame.display.get_num_displays
Return the number of displays
pygame.display.get_window_size
Return the size of the window or screen
pygame.display.get_window_position
Return the position of the window or screen
pygame.display.set_window_position
Set the current window position
pygame.display.get_allow_screensaver
Return whether the screensaver is allowed to run.
pygame.display.set_allow_screensaver
Set whether the screensaver may run
pygame.display.is_fullscreen
Returns True if the pygame window created by pygame.display.set_mode() is in full-screen
pygame.display.is_vsync
Returns True if vertical synchronisation for pygame.display.flip() and pygame.display.update
pygame.display.get_current_refresh_rate
Returns the screen refresh rate or 0 if unknown
pygame.display.get_desktop_refresh_rates
Returns the screen refresh rates for all displays (in windowed mode).
pygame.display.message_box
Create a native GUI message box
This module offers control over the pygame display. Pygame has a single display Surface that
is either contained in a window or runs full screen. Once you create the display you treat it as a
regular Surface. Changes are not immediately visible onscreen; you must choose one of the
two flipping functions to update the actual display.
The origin of the display, where x = 0 and y = 0, is the top left of the screen. Both axes increase
positively towards the bottom right of the screen.
The pygame display can actually be initialized in one of several modes. By default, the display
is a basic software driven framebuffer. You can request special modules like automatic scaling
or OpenGL support. These are controlled by flags passed to pygame.display.set_mode() .
Pygame can only have a single display active at any time. Creating a new one with
pygame.display.set_mode() will close the previous display. To detect the number and size
of attached screens, you can use pygame.display.get_desktop_sizes and then select ap-
propriate window size and display index to pass to pygame.display.set_mode() .
For backward compatibility pygame.display allows precise control over the pixel format or
display resolutions. This used to be necessary with old graphics cards and CRT screens, but is
usually not needed any more. Use the functions pygame.display.mode_ok() ,
pygame.display.list_modes() , and pygame.display.Info() to query detailed informa-
tion about the display.
Once the display Surface is created, the functions from this module affect the single existing
display. The Surface becomes invalid if the module is uninitialized. If a new display mode is
set, the existing Surface will automatically switch to operate on the new display.
When the display mode is set, several events are placed on the pygame event queue.
pygame.QUIT is sent when the user has requested the program to shut down. The window will
receive pygame.ACTIVEEVENT events as the display gains and loses input focus. If the display
is set with the pygame.RESIZABLE flag, pygame.VIDEORESIZE events will be sent when the
user adjusts the window dimensions. Hardware displays that draw direct to the screen will get
pygame.VIDEOEXPOSE events when portions of the window must be redrawn.
A new windowevent API was introduced in pygame 2.0.1. Check event module docs for more
information on that
Some display environments have an option for automatically stretching all windows. When this
option is enabled, this automatic stretching distorts the appearance of the pygame window. In
the pygame examples directory, there is example code (prevent_display_stretching.py) which
shows how to disable this automatic stretching of the pygame display on Microsoft Windows
(Vista or newer required).
pygame.display.init()
Initialize the display module
init() -> None
Initializes the pygame display module. The display module cannot do anything until it is ini-
tialized. This is usually handled for you automatically when you call the higher level
pygame.init() .
Pygame will select from one of several internal display backends when it is initialized. The
display mode will be chosen depending on the platform and permissions of current user.
Before the display module is initialized the environment variable SDL_VIDEODRIVER can
be set to control which backend is used. The systems with multiple choices are listed here.
Note: On wayland desktops, pygame-ce may choose to use the X11 video driver to run
on Xwayland. This behaviour is determined by the SDL library and might change
in the future, so it's suggested to account for this and not rely on the default be-
havior. The Wayland video driver can be forced by setting the SDL_VIDEODRIVER
environment variable to "wayland"
On some platforms it is possible to embed the pygame display into an already existing win-
dow. To do this, the environment variable SDL_WINDOWID must be set to a string contain-
ing the window id or handle. The environment variable is checked when the pygame dis-
play is initialized. Be aware that there can be many strange side effects when running in an
embedded display.
It is harmless to call this more than once, repeated calls have no effect.
Changed in pygame-ce 2.5.0: the manylinux wheels distributed by us now support the
wayland videodriver
pygame.display.quit()
Uninitialize the display module
quit() -> None
This will shut down the entire display module. This means any active displays will be
closed. This will also be handled automatically when the program exits.
It is harmless to call this more than once, repeated calls have no effect.
pygame.display.get_init()
Returns True if the display module has been initialized
get_init() -> bool
pygame.display.set_mode()
Initialize a window or screen for display
set_mode(size=(0, 0), flags=0, depth=0, display=0, vsync=0) -> Surface
This will create a window or display output and return a display Surface. The arguments
passed in are requests for a display type. The actual created display will be the best possi-
ble match supported by the system.
Note that calling this function implicitly initializes pygame.display , if it was not initialized
before.
The size argument is a pair of numbers representing the width and height. The flags argu-
ment is a collection of additional options. The depth argument represents the number of
bits to use for color.
The Surface that gets returned can be drawn to like a regular Surface but changes will
eventually be seen on the monitor.
If no size is passed or is set to (0, 0) , the created Surface will have the same size as the
current screen resolution. If only the width or height are set to 0 , the Surface will have the
same width or height as the screen resolution.
Since pygame 2, the depth argument is ignored, in favour of the best and fastest one. It
also raises a deprecation warning since pygame-ce 2.4.0 if the passed in depth is not 0 or
the one pygame selects.
When requesting fullscreen display modes, sometimes an exact match for the requested
size cannot be made. In these situations pygame will select the closest compatible match.
The returned surface will still always match the requested size.
On high resolution displays(4k, 1080p) and tiny graphics games (640x480) show up very
small so that they are unplayable. SCALED scales up the window for you. The game
thinks it's a 640x480 window, but really it can be bigger. Mouse events are scaled for you,
so your game doesn't need to do it. Note that SCALED is considered an experimental API
and may change in future releases.
The flags argument controls which type of display you want. There are several to choose
from, and you can even combine multiple types using the bitwise or operator, (the pipe "|"
character). Here are the display flags you will want to choose from:
pygame.FULLSCREEN create a fullscreen display
pygame.DOUBLEBUF only applicable with OPENGL
pygame.HWSURFACE (obsolete in pygame 2) hardware accelerated, only i
pygame.OPENGL create an OpenGL-renderable display
pygame.RESIZABLE display window should be sizeable
pygame.NOFRAME display window will have no border or controls
pygame.SCALED resolution depends on desktop size and scale graphi
pygame.SHOWN window is opened in visible mode (default)
pygame.HIDDEN window is opened in hidden mode
The display index 0 means the default display is used. If no display index argument is
provided, the default display can be overridden with an environment variable.
Changed in pygame 1.9.5: display argument added
Changed in pygame-ce 2.1.3: pygame now ensures that subsequent calls to this function
clears the window to black. On older versions, this was an implementation detail on the
major platforms this function was tested with.
pygame.display.get_surface()
Get a reference to the currently set display surface
get_surface() -> Surface
get_surface() -> None
Return a reference to the currently set display Surface. If no display mode has been set
this will return None.
pygame.display.flip()
Update the full display Surface to the screen
flip() -> None
pygame.display.update()
Update all, or a portion, of the display. For non-OpenGL displays.
update() -> None
update(rectangle, /) -> None
update(x, y, w, h, /) -> None
update((x, y), (w, h), /) -> None
update(rectangle_iterable, /) -> None
You can pass the function a single rectangle, or an iterable of rectangles. Generally you do
not want to pass an iterable of rectangles as there is a performance cost per rectangle
passed to the function. On modern hardware, after a very small number of rectangles
passed in, the per-rectangle cost will exceed the saving of updating less pixels. In most ap-
plications it is simply more efficient to update the entire display surface at once, it also
means you do not need to keep track of a list of rectangles for each call to update.
If passing an iterable of rectangles it is safe to include None values in the list, which will be
skipped.
This call cannot be used on pygame.OPENGL displays and will generate an exception.
Changed in pygame-ce 2.5.1: Added support for passing an iterable, previously only se-
quence was allowed
pygame.display.get_driver()
Get the name of the pygame display backend
get_driver() -> name
Pygame chooses one of many available display backends when it is initialized. This re-
turns the internal name used for the display backend. This can be used to provide limited
information about what display capabilities might be accelerated. See the
SDL_VIDEODRIVER flags in pygame.display.set_mode() to see some of the common
options.
pygame.display.Info()
Create a video display information object
Info() -> VideoInfo
Creates a simple object containing several attributes to describe the current graphics envi-
ronment. If this is called before pygame.display.set_mode() some platforms can pro-
vide information about the default display mode. This can also be called after setting the
display mode to verify specific display options were satisfied. The VidInfo object has sev-
eral attributes:
pygame.display.get_wm_info()
Get information about the current windowing system
get_wm_info() -> dict
Creates a dictionary filled with string keys. The strings and values are arbitrarily created by
the system. Some systems may have no information and an empty dictionary will be re-
turned. Most platforms will return a "window" key with the value set to the system id for the
current display.
New in pygame 1.7.1.
pygame.display.get_desktop_sizes()
Get sizes of active desktops
get_desktop_sizes() -> list
This function returns the sizes of the currently configured virtual desktops as a list of (x, y)
tuples of integers.
The length of the list is not the same as the number of attached monitors, as a desktop
can be mirrored across multiple monitors. The desktop sizes do not indicate the maximum
monitor resolutions supported by the hardware, but the desktop size configured in the op-
erating system.
In order to fit windows into the desktop as it is currently configured, and to respect the res-
olution configured by the operating system in fullscreen mode, this function should be used
to replace many use cases of pygame.display.list_modes() whenever applicable.
New in pygame 2.0.0.
pygame.display.list_modes()
Get list of available fullscreen modes
list_modes(depth=0, flags=pygame.FULLSCREEN, display=0) -> list
This function returns a list of possible sizes for a specified color depth. The return value
will be an empty list if no display modes are available with the given arguments. A return
value of -1 means that any requested size should work (this is likely the case for win-
dowed modes). Mode sizes are sorted from biggest to smallest.
If depth is 0 , the current/best color depth for the display is used. The flags defaults to
pygame.FULLSCREEN , but you may need to add additional flags for specific fullscreen
modes.
The display index 0 means the default display is used.
Since pygame 2.0, pygame.display.get_desktop_sizes() has taken over some use
cases from pygame.display.list_modes() :
To find a suitable size for non-fullscreen windows, it is preferable to use
pygame.display.get_desktop_sizes() to get the size of the current desktop, and to
then choose a smaller window size. This way, the window is guaranteed to fit, even when
the monitor is configured to a lower resolution than the maximum supported by the
hardware.
To avoid changing the physical monitor resolution, it is also preferable to use
pygame.display.get_desktop_sizes() to determine the fullscreen resolution.
Developers are strongly advised to default to the current physical monitor resolution unless
the user explicitly requests a different one (e.g. in an options menu or configuration file).
Changed in pygame 1.9.5: display argument added
pygame.display.mode_ok()
Pick the best color depth for a display mode
mode_ok(size, flags=0, depth=0, display=0) -> depth
pygame.display.gl_get_attribute()
Get the value for an OpenGL flag for the current display
gl_get_attribute(flag, /) -> value
pygame.display.gl_set_attribute()
Request an OpenGL display attribute for the display mode
gl_set_attribute(flag, value, /) -> None
GL_MULTISAMPLEBUFFERS
Whether to enable multisampling anti-aliasing. Defaults to 0 (disabled).
Set GL_MULTISAMPLESAMPLES to a value above 0 to control the amount of anti-alias-
ing. A typical value is 2 or 3.
GL_STENCIL_SIZE
Minimum bit size of the stencil buffer. Defaults to 0.
GL_DEPTH_SIZE
Minimum bit size of the depth buffer. Defaults to 16.
GL_STEREO
1 enables stereo 3D. Defaults to 0.
GL_BUFFER_SIZE
Minimum bit size of the frame buffer. Defaults to 0.
New in pygame 2.0.0: Additional attributes:
GL_ACCELERATED_VISUAL,
GL_CONTEXT_MAJOR_VERSION, GL_CONTEXT_MINOR_VERSION,
GL_CONTEXT_FLAGS, GL_CONTEXT_PROFILE_MASK,
GL_SHARE_WITH_CURRENT_CONTEXT,
GL_CONTEXT_RELEASE_BEHAVIOR,
GL_FRAMEBUFFER_SRGB_CAPABLE
GL_CONTEXT_PROFILE_MASK
Sets the OpenGL profile to one of these values:
GL_ACCELERATED_VISUAL
Set to 1 to require hardware acceleration, or 0 to force software render. By default,
both are allowed.
pygame.display.get_active()
Returns True when the display is active on the screen
get_active() -> bool
Returns True when the display Surface is considered actively renderable on the screen
and may be visible to the user. This is the default state immediately after
pygame.display.set_mode() . This method may return True even if the application is
fully hidden behind another application window.
This will return False if the display Surface has been iconified or minimized (either via
pygame.display.iconify() or via an OS specific method such as the minimize-icon
available on most desktops).
The method can also return False for other reasons without the application being explicitly
iconified or minimized by the user. A notable example being if the user has multiple virtual
desktops and the display Surface is not on the active virtual desktop.
Note: This function returning True is unrelated to whether the application has input fo-
cus. Please see pygame.key.get_focused() and pygame.mouse.get_focused()
for APIs related to input focus.
pygame.display.iconify()
Iconify the display surface
iconify() -> bool
Request the window for the display surface be iconified or hidden. Not all systems and dis-
plays support an iconified display. The function will return True if successful.
When the display is iconified pygame.display.get_active() will return False . The
event queue should receive an ACTIVEEVENT event when the window has been iconified.
Additionally, the event queue also receives a WINDOWEVENT_MINIMIZED event when the
window has been iconified on pygame 2.
pygame.display.toggle_fullscreen()
Switch between fullscreen and windowed displays
toggle_fullscreen() -> int
Switches the display window between windowed and fullscreen modes. Display driver sup-
port is not great when using pygame 1, but with pygame 2 it is the most reliable method to
switch to and from fullscreen.
Supported display drivers in pygame 1:
x11 (Linux/Unix)
wayland (Linux/Unix)
pygame.display.set_gamma()
Change the hardware gamma ramps
set_gamma(red, green=None, blue=None, /) -> bool
pygame.display.set_gamma_ramp()
Change the hardware gamma ramps with a custom lookup
set_gamma_ramp(red, green, blue, /) -> bool
pygame.display.set_icon()
Change the system image for the display window
set_icon(surface, /) -> None
Sets the runtime icon the system will use to represent the display window. All windows de-
fault to a simple pygame logo for the window icon.
Note that calling this function implicitly initializes pygame.display , if it was not initialized
before.
You can pass any surface, but most systems want a smaller image around 32x32. The im-
age can have colorkey transparency which will be passed to the system.
Some systems do not allow the window icon to change after it has been shown. This func-
tion can be called before pygame.display.set_mode() to create the icon before the dis-
play mode is set.
pygame.display.set_caption()
Set the current window caption
set_caption(title, icontitle=None, /) -> None
If the display has a window title, this function will change the name on the window. In
pygame 1.x, some systems supported an alternate shorter title to be used for minimized
displays, but in pygame 2 icontitle does nothing.
pygame.display.get_caption()
Get the current window caption
get_caption() -> (title, icontitle)
Returns the title and icontitle of the display window. In pygame 2.x these will always be the
same value.
pygame.display.set_palette()
Set the display color palette for indexed displays
set_palette(palette=None, /) -> None
This will change the video display color palette for 8-bit displays. This does not change the
palette for the actual display Surface, only the palette that is used to display the Surface. If
no palette argument is passed, the system default palette will be restored. The palette is a
sequence of RGB triplets.
pygame.display.get_num_displays()
Return the number of displays
get_num_displays() -> int
pygame.display.get_window_size()
Return the size of the window or screen
get_window_size() -> tuple
Returns the size of the window initialized with pygame.display.set_mode() . This may
differ from the size of the display surface if SCALED is used.
New in pygame 2.0.0.
pygame.display.get_window_position()
Return the position of the window or screen
get_window_position() -> tuple
Returns the position of the window initialized with pygame.display.set_mode() . The po-
sition will change when the user moves the window or when the position is set manually
with pygame.display.set_window_position() . Coordinates could be negative or out-
side the desktop size bounds. The position is relative to the topleft of the primary monitor
and the y coordinate ignores the window frame.
pygame.display.set_window_position()
Set the current window position
set_window_position((x, y)) -> None
Sets the position of the window initialized with pygame.display.set_mode() . This differs
from updating environment variables as this function can be called after the display has
been initialised. The position is expected to be relative to the topleft of the primary monitor.
The y coordinate will ignore the window frame (y = 0 means the frame is hidden). The user
will still be able to move the window after this call. See also
pygame.display.get_window_position() .
pygame.display.get_allow_screensaver()
Return whether the screensaver is allowed to run.
get_allow_screensaver() -> bool
Return whether screensaver is allowed to run whilst the app is running. Default is False .
By default pygame does not allow the screensaver during game play.
Note: Some platforms do not have a screensaver or support disabling the screensaver.
Please see pygame.display.set_allow_screensaver() for caveats with screen-
saver support.
pygame.display.set_allow_screensaver()
Set whether the screensaver may run
set_allow_screensaver(bool) -> None
Change whether screensavers should be allowed whilst the app is running. The default
value of the argument to the function is True. By default pygame does not allow the
screensaver during game play.
If the screensaver has been disallowed due to this function, it will automatically be allowed
to run when pygame.quit() is called.
It is possible to influence the default value via the environment variable
SDL_HINT_VIDEO_ALLOW_SCREENSAVER , which can be set to either 0 (disable) or 1
(enable).
pygame.display.is_fullscreen()
Returns True if the pygame window created by pygame.display.set_mode() is in full-screen
mode
is_fullscreen() -> bool
Edge cases: If the window is in windowed mode, but maximized, this will return False. If
the window is in "borderless fullscreen" mode, this will return True.
New in pygame-ce 2.2.0.
pygame.display.is_vsync()
Returns True if vertical synchronisation for pygame.display.flip() and
pygame.display.update() is enabled
is_vsync() -> bool
pygame.display.get_current_refresh_rate() → int
Returns the screen refresh rate or 0 if unknown
get_current_refresh_rate() -> int
The screen refresh rate for the current window. In windowed mode, this should be equal to
the refresh rate of the desktop the window is on.
If no window is open, an exception is raised.
When a constant refresh rate cannot be determined, 0 is returned.
New in pygame-ce 2.2.0.
pygame.display.get_desktop_refresh_rates() → list
Returns the screen refresh rates for all displays (in windowed mode).
get_desktop_refresh_rates() -> list
If the current window is in full-screen mode, the actual refresh rate for that window can
differ.
This is safe to call when no window is open (i.e. before any calls to
pygame.display.set_mode()
When a constant refresh rate cannot be determined, 0 is returned for that desktop.
New in pygame-ce 2.2.0.
pygame.display.message_box()
Create a native GUI message box
message_box(title, message=None, message_type='info', parent_window=None, buttons=
('OK',), return_button=0, escape_button=None) -> int
This function should be called on the thread that set_mode() is called. It will block execu-
tion of that thread until the user clicks a button or closes the message_box.
This function may be called at any time, even before pygame.init() .
Negative values of return_button and escape_button are allowed just like standard
Python list indexing.
New in pygame-ce 2.4.0.
Edit on GitHub