Debian Codegraph @

doc/programming_guide/windowing.rst

raw
1 Windowing
2 =========
3
4 A :py:class:`~pyglet.window.Window` in pyglet corresponds to a top-level
5 window as provided by the operating system. Windows can be floating (with or
6 without a border), or fullscreen.
7
8 .. _guide_creating-a-window:
9
10 Creating a window
11 -----------------
12
13 If the :py:class:`~pyglet.window.Window` constructor is called with no
14 arguments, defaults will be assumed for all parameters::
15
16 window = pyglet.window.Window()
17
18 The default parameters used are:
19
20 * The window will have a size of 960x540, and not be resizable.
21 * A default context will be created using template config described in
22 :ref:`guide_glconfig`.
23 * The window caption will be the name of the executing Python script
24 (i.e., ``sys.argv[0]``).
25
26 Windows are visible as soon as they are created, unless you give the
27 ``visible=False`` argument to the constructor. The following
28 example shows how to create and display a window in two steps::
29
30 window = pyglet.window.Window(visible=False)
31 # ... perform some additional initialisation
32 window.set_visible()
33
34 OpenGL Context configuration
35 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
36
37 The context of a window cannot be changed once created. There are several
38 ways to control the context that is created:
39
40 * Supply an already-created :py:class:`~pyglet.gl.Context` using the
41 ``context`` argument::
42
43 context = config.create_context(share)
44 window = pyglet.window.Window(context=context)
45
46 * Supply a complete :py:class:`~pyglet.gl.Config` obtained from a
47 :py:class:`~pyglet.display.Screen` using the ``config``
48 argument. The context will be created from this config and will share object
49 space with the most recently created existing context::
50
51 display = pyglet.display.get_display()
52 screen = display.get_default_screen()
53 config = screen.get_best_config(template)
54 window = pyglet.window.Window(config=config)
55
56 * Supply a template :py:class:`~pyglet.gl.Config` using the ``config``
57 argument. The context will use the best config obtained from the default
58 screen of the default display::
59
60 config = gl.Config(double_buffer=True)
61 window = pyglet.window.Window(config=config)
62
63 * Specify a :py:class:`~pyglet.display.Screen` using the ``screen`` argument.
64 The context will use a config created from default template configuration
65 and this screen::
66
67 display = pyglet.display.get_display()
68 screen = display.get_screens()[screen_number]
69 window = pyglet.window.Window(screen=screen)
70
71 * Specify a :py:class:`~pyglet.display.Display` using the ``display`` argument.
72 The default screen on this display will be used to obtain a context using
73 the default template configuration::
74
75 display = platform.get_display(display_name)
76 window = pyglet.window.Window(display=display)
77
78 If a template :py:class:`~pyglet.gl.Config` is given, a
79 :py:class:`~pyglet.display.Screen` or :py:class:`~pyglet.display.Display`
80 may also be specified; however any other combination of parameters
81 overconstrains the configuration and some parameters will be ignored.
82
83 Fullscreen windows
84 ^^^^^^^^^^^^^^^^^^
85
86 If the ``fullscreen=True`` argument is given to the window constructor, the
87 window will draw to an entire screen rather than a floating window. No window
88 border or controls will be shown, so you must ensure you provide some other
89 means to exit the application.
90
91 By default, the default screen on the default display will be used, however
92 you can optionally specify another screen to use instead. For example, the
93 following code creates a fullscreen window on the secondary screen::
94
95 screens = display.get_screens()
96 window = pyglet.window.Window(fullscreen=True, screen=screens[1])
97
98 There is no way to create a fullscreen window that spans more than one window
99 (for example, if you wanted to create an immersive 3D environment across
100 multiple monitors). Instead, you should create a separate fullscreen window
101 for each screen and attach identical event handlers to all windows.
102
103 Windows can be toggled in and out of fullscreen mode with the
104 :py:meth:`~pyglet.window.Window.set_fullscreen`
105 method. For example, to return to windowed mode from fullscreen::
106
107 window.set_fullscreen(False)
108
109 The previous window size and location, if any, will attempt to be restored,
110 however the operating system does not always permit this, and the window may
111 have relocated.
112
113 Size and position
114 ^^^^^^^^^^^^^^^^^
115
116 This section applies only to windows that are not fullscreen. Fullscreen
117 windows always have the width and height of the screen they fill.
118
119 You can specify the size of a window as the first two arguments to the window
120 constructor. In the following example, a window is created with a width of
121 1280 pixels and a height of 720 pixels::
122
123 window = pyglet.window.Window(1280, 720)
124
125 The "size" of a window refers to the drawable space within it, excluding any
126 additional borders or title bar drawn by the operating system.
127
128 You can allow the user to resize your window by specifying ``resizable=True``
129 in the constructor. If you do this, you may also want to handle the
130 :py:meth:`~pyglet.window.Window.on_resize` event::
131
132 window = pyglet.window.Window(resizable=True)
133
134 @window.event
135 def on_resize(width, height):
136 print(f'The window was resized to {width},{height}')
137
138 You can specify a minimum and maximum size that the window can be resized to
139 by the user with the :py:meth:`~pyglet.window.Window.set_minimum_size` and
140 :py:meth:`~pyglet.window.Window.set_maximum_size` methods::
141
142 window.set_minimum_size(320, 200)
143 window.set_maximum_size(1024, 768)
144
145 The window can also be resized programmatically (even if the window is not
146 user-resizable) with the :py:meth:`~pyglet.window.Window.set_size` method::
147
148 window.set_size(1280, 720)
149
150 The window will initially be positioned by the operating system. Typically,
151 it will use its own algorithm to locate the window in a place that does not
152 block other application windows, or cascades with them. You can manually
153 adjust the position of the window using the
154 :py:meth:`~pyglet.window.Window.get_location` and
155 :py:meth:`~pyglet.window.Window.set_location` methods::
156
157 x, y = window.get_location()
158 window.set_location(x + 20, y + 20)
159
160 Note that unlike the usual coordinate system in pyglet, the window location is
161 relative to the top-left corner of the desktop, as shown in the following
162 diagram:
163
164 .. figure:: img/window_location.png
165
166 The position and size of the window relative to the desktop.
167
168 Appearance
169 ----------
170
171 Window style
172 ^^^^^^^^^^^^
173
174 Non-fullscreen windows can be created in one of six styles: default, dialog,
175 tool, borderless, transparent, or overlay. Transparent and overlay windows both
176 set a transparent framebuffer if the graphics card and windowing system both allow it.
177
178
179 .. list-table::
180 :header-rows: 1
181
182 * - Style
183 - Windows
184 - Mac OS X
185 * - :py:attr:`~pyglet.window.Window.WINDOW_STYLE_DEFAULT`
186 - .. image:: img/window_xp_default.png
187 - .. image:: img/window_osx_default.png
188 * - :py:attr:`~pyglet.window.Window.WINDOW_STYLE_DIALOG`
189 - .. image:: img/window_xp_dialog.png
190 - .. image:: img/window_osx_dialog.png
191 * - :py:attr:`~pyglet.window.Window.WINDOW_STYLE_TOOL`
192 - .. image:: img/window_xp_tool.png
193 - .. image:: img/window_osx_tool.png
194 * - :py:attr:`~pyglet.window.Window.WINDOW_STYLE_BORDERLESS`
195 - <Image Not Available>
196 - .. image:: img/window_osx_borderless.png
197 * - :py:attr:`~pyglet.window.Window.WINDOW_STYLE_TRANSPARENT`
198 - .. image:: img/window_xp_transparent.png
199 - Implemented, not pictured.
200 * - :py:attr:`~pyglet.window.Window.WINDOW_STYLE_OVERLAY`
201 - .. image:: img/window_xp_overlay.png
202 - Implemented, not pictured.
203
204 Non-resizable variants of these window styles may appear slightly different
205 (for example, the maximize button will either be disabled or absent).
206
207 Besides the change in appearance, the window styles affect how the window
208 behaves. For example, tool windows do not usually appear in the task bar and
209 cannot receive keyboard focus. Dialog windows cannot be minimized.
210
211 Choosing the appropriate window style for your window means your application should
212 behave correctly for the platform on which it is running. However, keep in mind, certain window
213 behaviour is ultimately controlled by the underlying operating system and may have unexpected behavior
214 depending on your usage. It would be pragmatic to test your use case on all operating systems you plan to
215 support with your application.
216
217 The appearance and behaviour of windows in Linux will vary greatly depending
218 on the distribution, window manager and user preferences.
219
220 Overlay windows (:py:attr:`~pyglet.window.Window.WINDOW_STYLE_OVERLAY`)
221 require custom sizing and moving of the respective window. By default, overlay's are always on top,
222 and do not accept mouse clicks.
223
224 Borderless windows (:py:attr:`~pyglet.window.Window.WINDOW_STYLE_BORDERLESS`)
225 are not decorated by the operating system at all, and have no way to be resized
226 or moved around the desktop. These are useful for implementing splash screens
227 or custom window borders.
228
229 You can specify the style of the window in the
230 :py:class:`~pyglet.window.Window` constructor.
231 Once created, the window style cannot be altered::
232
233 window = pyglet.window.Window(style=pyglet.window.Window.WINDOW_STYLE_DIALOG)
234
235 Caption
236 ^^^^^^^
237
238 The window's caption appears in its title bar and task bar icon (on Windows
239 and some Linux window managers). You can set the caption during window
240 creation or at any later time using the
241 :py:meth:`~pyglet.window.Window.set_caption` method::
242
243 window = pyglet.window.Window(caption='Initial caption')
244 window.set_caption('A different caption')
245
246 Icon
247 ^^^^
248
249 The window icon appears in the title bar and task bar icon on Windows and
250 Linux, and in the dock icon on Mac OS X. Dialog and tool windows do not
251 necessarily show their icon.
252
253 Windows, Mac OS X and the Linux window managers each have their own preferred
254 icon sizes:
255
256 Windows XP
257 * A 16x16 icon for the title bar and task bar.
258 * A 32x32 icon for the Alt+Tab switcher.
259 Mac OS X
260 * Any number of icons of resolutions 16x16, 24x24, 32x32, 48x48, 72x72
261 and 128x128. The actual image displayed will be interpolated to the
262 correct size from those provided.
263 Linux
264 * No constraints, however most window managers will use a 16x16 and a
265 32x32 icon in the same way as Windows XP.
266
267 The :py:meth:`~pyglet.window.Window.set_icon` method allows you to set any
268 number of images as the icon.
269 pyglet will select the most appropriate ones to use and apply them to
270 the window. If an alternate size is required but not provided, pyglet will
271 scale the image to the correct size using a simple interpolation algorithm.
272
273 The following example provides both a 16x16 and a 32x32 image as the window
274 icon::
275
276 window = pyglet.window.Window()
277 icon1 = pyglet.image.load('16x16.png')
278 icon2 = pyglet.image.load('32x32.png')
279 window.set_icon(icon1, icon2)
280
281 You can use images in any format supported by pyglet, however it is
282 recommended to use a format that supports alpha transparency such as PNG.
283 Windows .ico files are supported only on Windows, so their use is discouraged.
284 Mac OS X .icons files are not supported at all.
285
286 Note that the icon that you set at runtime need not have anything to do with
287 the application icon, which must be encoded specially in the application
288 binary (see `Self-contained executables`).
289
290 Visibility
291 ----------
292
293 Windows have several states of visibility. Already shown is the
294 :py:attr:`~pyglet.window.Window.visible` property which shows or hides
295 the window.
296
297 Windows can be minimized, which is equivalent to hiding them except that
298 they still appear on the taskbar (or are minimised to the dock, on OS X).
299 The user can minimize a window by clicking the appropriate button in the
300 title bar.
301 You can also programmatically minimize a window using the
302 :py:class:`~pyglet.window.Window.minimize` method (there is also a
303 corresponding :py:class:`~pyglet.window.Window.maximize` method).
304
305 When a window is made visible the :py:meth:`~pyglet.window.Window.on_show`
306 event is triggered. When it is hidden the
307 :py:meth:`~pyglet.window.Window.on_hide` event is triggered.
308 On Windows and Linux these events
309 will only occur when you manually change the visibility of the window or when
310 the window is minimized or restored. On Mac OS X the user can also hide or
311 show the window (affecting visibility) using the Command+H shortcut.
312
313 .. _guide_subclassing-window:
314
315 Subclassing Window
316 ------------------
317
318 A useful pattern in pyglet is to subclass :py:class:`~pyglet.window.Window` for
319 each type of window you will display, or as your main application class. There
320 are several benefits:
321
322 * You can load font and other resources from the constructor, ensuring the
323 OpenGL context has already been created.
324 * You can add event handlers simply by defining them on the class. The
325 :py:meth:`~pyglet.window.Window.on_resize` event will be called as soon as
326 the window is created (this
327 doesn't usually happen, as you must create the window before you can attach
328 event handlers).
329 * There is reduced need for global variables, as you can maintain application
330 state on the window.
331
332 The following example shows the same "Hello World" application as presented
333 in :ref:`quickstart`, using a subclass of :py:class:`~pyglet.window.Window`::
334
335 class HelloWorldWindow(pyglet.window.Window):
336 def __init__(self):
337 super().__init__()
338
339 self.label = pyglet.text.Label('Hello, world!')
340
341 def on_draw(self):
342 self.clear()
343 self.label.draw()
344
345 if __name__ == '__main__':
346 window = HelloWorldWindow()
347 pyglet.app.run()
348
349 This example program is located in
350 ``examples/programming_guide/window_subclass.py``.
351
352 Windows and OpenGL contexts
353 ---------------------------
354
355 Every window in pyglet has an associated OpenGL context.
356 Specifying the configuration of this context has already been covered in
357 :ref:`guide_creating-a-window`.
358 Drawing into the OpenGL context is the only way to draw into the window's
359 client area.
360
361 Double-buffering
362 ^^^^^^^^^^^^^^^^
363
364 If the window is double-buffered (i.e., the configuration specified
365 ``double_buffer=True``, the default), OpenGL commands are applied to a hidden
366 back buffer. This back buffer can be brought to the front using the `flip`
367 method. The previous front buffer then becomes the hidden back buffer
368 we render to in the next frame. If you are using the standard `pyglet.app.run`
369 or :py:class:`pyglet.app.EventLoop` event loop, this is taken care of
370 automatically after each :py:meth:`~pyglet.window.Window.on_draw` event.
371
372 If the window is not double-buffered, the
373 :py:meth:`~pyglet.window.Window.flip` operation is unnecessary,
374 and you should remember only to call :py:func:`pyglet.gl.glFlush` to
375 ensure buffered commands are executed.
376
377 Vertical retrace synchronisation
378 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
379
380 Double-buffering eliminates one cause of flickering: the user is unable to see
381 the image as it is painted, only the final rendering. However, it does introduce
382 another source of flicker known as "tearing".
383
384 Tearing becomes apparent when displaying fast-moving objects in an animation.
385 The buffer flip occurs while the video display is still reading data from the
386 framebuffer, causing the top half of the display to show the previous frame
387 while the bottom half shows the updated frame. If you are updating the
388 framebuffer particularly quickly you may notice three or more such "tears" in
389 the display.
390
391 pyglet provides a way to avoid tearing by synchronising buffer flips to the
392 video refresh rate. This is enabled by default, but can be set or unset
393 manually at any time with the :py:attr:`~pyglet.window.Window.vsync` (vertical
394 retrace synchronisation)
395 property. A window is created with vsync initially disabled in the following
396 example::
397
398 window = pyglet.window.Window(vsync=False)
399
400 It is usually desirable to leave vsync enabled, as it results in flicker-free
401 animation. There are some use-cases where you may want to disable it, for
402 example:
403
404 * Profiling an application. Measuring the time taken to perform an operation
405 will be affected by the time spent waiting for the video device to refresh,
406 which can throw off results. You should disable vsync if you are measuring
407 the performance of your application.
408 * If you cannot afford for your application to block. If your application run
409 loop needs to quickly poll a hardware device, for example, you may want to
410 avoid blocking with vsync.