058-python库pyglet(三)

续上,文章内容过长,不能发布,只能分开了~

三、Windowing(窗口)

A Window in pyglet corresponds to a top-level window provided by the operating system. Windows can be floating (overlapped with other application windows) or fullscreen.

一个窗口在pyglet对应于一个顶级窗口由操作系统提供。Windows可以浮动(与其他应用程序窗口重叠)或全屏。

Creating a window(创建一个窗口)

If the Window constructor is called with no arguments, defaults will be assumed for all parameters:

如果窗口构造函数不带参数调用,违约将承担所有参数:

window = pyglet.window.Window()

The default parameters used are:

使用默认的参数是:

  • The window will have a size of 640x480, and not be resizable.

窗口大小为640 x480,而不是可调整大小的。

将会创建一个默认的上下文使用OpenGL中描述模板配置配置选项。

  • The window caption will be the name of the executing Python script (i.e., sys.argv[0]).

窗口标题将执行Python脚本的名称(即。,sys.argv [0])。

Windows are visible as soon as they are created, unless you give the visible=False argument to the constructor. The following example shows how to create and display a window in two steps:

窗户就可以看到了,除非你给可见= False参数的构造函数。下面的例子显示了如何创建并显示一个窗口在两个步骤:

window = pyglet.window.Window(visible=False)
# ... perform some additional initialisation
window.set_visible()

Context configuration(Context配置)

The context of a window cannot be changed once created. There are several ways to control the context that is created:

上下文中创建的窗口无法改变一次。这里有几种方法来控制创建的背景:

  • Supply an already-created Context using the context argument:

提供一个创建上下文使用上下文参数:

context = config.create_context(share)
window = pyglet.window.Window(context=context)
  • Supply a complete Config obtained from a Screen using the config argument. The context will be created from this config and will share object space with the most recently created existing context:

提供一个完整的配置从一个屏幕使用配置参数。从这个配置上下文将被创建,并将与最近创建的共享对象空间现有的背景:

config = screen.get_best_config(template)
window = pyglet.window.Window(config=config)
  • Supply a template Config using the config argument. The context will use the best config obtained from the default screen of the default display:

提供一个模板使用配置参数配置。上下文将使用最好的配置从默认的屏幕默认的显示:

config = gl.Config(double_buffer=True)
window = pyglet.window.Window(config=config)
  • Specify a Screen using the screen argument. The context will use a config created from default template configuration and this screen:

使用屏幕参数指定一个屏幕。上下文将使用一个配置创建的默认模板配置和这个屏幕:

screen = display.get_screens()[screen_number]
window = pyglet.window.Window(screen=screen)
  • Specify a Display using the display argument. The default screen on this display will be used to obtain a context using the default template configuration:

指定一个显示使用显示参数。默认的屏幕上显示将被用来获得一个上下文使用默认模板配置:

display = platform.get_display(display_name)
window = pyglet.window.Window(display=display)

If a template Config is given, a Screen or Display may also be specified; however any other combination of parameters overconstrains the configuration and some parameters will be ignored.

如果一个模板配置,一个屏幕或显示也可以指定;但是其他的组合参数overconstrains配置和一些参数将被忽略。

Fullscreen windows(全屏窗口)

If the fullscreen=True argument is given to the window constructor, the window will draw to an entire screen rather than a floating window. No window border or controls will be shown, so you must ensure you provide some other means to exit the application.

如果全屏= True参数窗口构造函数,给定窗口将整个屏幕,而不是一个浮动窗口。没有窗口的边界或控件将显示,所以你必须确保你提供一些其他方式退出应用程序。

By default, the default screen on the default display will be used, however you can optionally specify another screen to use instead. For example, the following code creates a fullscreen window on the secondary screen:

默认情况下,将使用默认的屏幕上默认显示,但是您可以指定另一个屏幕使用。例如,下面的代码创建了一个二级屏幕上全屏窗口:

screens = display.get_screens()
window = pyglet.window.Window(fullscreen=True, screen=screens[1])

There is no way to create a fullscreen window that spans more than one window (for example, if you wanted to create an immersive 3D environment across multiple monitors). Instead, you should create a separate fullscreen window for each screen and attach identical event handlers to all windows.

没有办法创建一个跨越多个窗口全屏窗口(例如,如果您想创建一个身临其境的3 d环境中跨多个监视器)。相反,您应该创建一个单独的全屏窗口为每个屏幕和相同的事件处理程序附加到所有窗户。

Windows can be toggled in and out of fullscreen mode with the set_fullscreen() method. For example, to return to windowed mode from fullscreen:

Windows可以连接在全屏模式与set_fullscreen()方法。例如,回到全屏窗口模式:

window.set_fullscreen(False)

The previous window size and location, if any, will attempt to be restored, however the operating system does not always permit this, and the window may have relocated.

前面的窗口的大小和位置,如果有的话,将尝试恢复,然而这个操作系统并不总是允许,和窗口可能会搬迁。

Size and position(大小和位置)

This section applies only to windows that are not fullscreen. Fullscreen windows always have the width and height of the screen they fill.

本节仅适用于windows不全屏。全屏窗口总是他们填满屏幕的宽度和高度。

You can specify the size of a window as the first two arguments to the window constructor. In the following example, a window is created with a width of 1280 pixels and a height of 720 pixels:

您可以指定一个窗口的大小的窗口前两个参数的构造函数。在下列的示例中,创建一个窗口宽度为1280像素和720像素的高度:

window = pyglet.window.Window(1280, 720)

The “size” of a window refers to the drawable space within it, excluding any additional borders or title bar drawn by the operating system.

窗口的“大小”指的是可拉的空间内,不包括任何额外的边界或标题栏的操作系统。

You can allow the user to resize your window by specifying resizable=True in the constructor. If you do this, you may also want to handle the on_resize() event:

你可以允许用户调整窗口通过指定在构造函数中可调整大小的= True。如果你这样做,你也可以处理on_resize()事件:

window = pyglet.window.Window(resizable=True)

@window.event
def on_resize(width, height):
    print 'The window was resized to %dx%d' % (width, height)

You can specify a minimum and maximum size that the window can be resized to by the user with the set_minimum_size()and set_maximum_size() methods:

您可以指定一个最小值和最大值大小可以调整窗口大小以便用户与set_minimum_size()和set_maximum_size()方法:

window.set_minimum_size(320, 200)
window.set_maximum_size(1024, 768)

The window can also be resized programatically (even if the window is not user-resizable) with the set_size() method:

窗口还可以调整大小编程(即使窗口不是user-resizable)与set_size()方法:

window.set_size(1280, 720)

The window will initially be positioned by the operating system. Typically, it will use its own algorithm to locate the window in a place that does not block other application windows, or cascades with them. You can manually adjust the position of the window using the get_location() and set_location() methods:

窗口最初将定位由操作系统。通常情况下,它将使用自己的算法来定位窗口在一个地方不会阻止其他应用程序窗口,或级联。您可以手动调整窗口的位置使用get_location()和set_location()方法:

x, y = window.get_location()
window.set_location(x + 20, y + 20)

Note that unlike the usual coordinate system in pyglet, the window location is relative to the top-left corner of the desktop, as shown in the following diagram:

注意,与pyglet常用的坐标系不同,窗口的位置是相对于桌面的左上角,如下图所示:

image.png

The position and size of the window relative to the desktop.

窗口的位置和大小相对于桌面。

Appearance(外观)

Window style(窗口样式)

Non-fullscreen windows can be created in one of four styles: default, dialog, tool or borderless. Examples of the appearances of each of these styles under Windows and Mac OS X 10.4 are shown below.

非全屏窗口可以创建的四种形式:默认情况下,对话框、工具或无国界。表象下的这些风格的例子Windows和Mac OS X 10.4如下所示。

image.png

Non-resizable variants of these window styles may appear slightly different (for example, the maximize button will either be disabled or absent).

Non-resizable这些窗口风格的变异可能出现略有不同(例如,最大化按钮将被禁用或缺席)。

Besides the change in appearance, the window styles affect how the window behaves. For example, tool windows do not usually appear in the task bar and cannot receive keyboard focus. Dialog windows cannot be minimized. Selecting the appropriate window style for your windows means your application will behave correctly for the platform on which it is running, however that behaviour may not be consistent across Windows, Linux and Mac OS X.

除了外观上的改变,窗口样式影响窗口的行为。例如,工具窗口通常不出现在任务栏,不能接收键盘焦点。对话框不能最小化窗口。选择合适的窗口风格为你的windows意味着应用程序将表现出正确的平台运行,但是这种行为可能不是一致windows, Linux和Mac OS X。

The appearance and behaviour of windows in Linux will vary greatly depending on the distribution, window manager and user preferences.

在Linux中窗口的外观和行为将根据分布的不同,有很大变化,窗口管理器和用户首选项。

Borderless windows (WINDOW_STYLE_BORDERLESS) are not decorated by the operating system at all, and have no way to be resized or moved around the desktop. These are useful for implementing splash screens or custom window borders.

无国界的窗口(WINDOW_STYLE_BORDERLESS)由操作系统不是装饰,和没有办法调整大小或移动桌面。这些都是有用的或自定义实现闪屏窗口边界。

You can specify the style of the window in the Window constructor. Once created, the window style cannot be altered:

您可以指定窗口的窗口的风格的构造函数。一旦创建,窗口风格不能修改:

window = pyglet.window.Window(style=window.Window.WINDOW_STYLE_DIALOG)

Caption(标题)

The window’s caption appears in its title bar and task bar icon (on Windows and some Linux window managers). You can set the caption during window creation or at any later time using the set_caption() method:

窗口的标题出现在其标题栏,任务栏图标(在Windows和Linux窗口管理器)。你可以设置标题窗口创建期间或以后在任何时间使用set_caption()方法:

window = pyglet.window.Window(caption='Initial caption')
window.set_caption('A different caption')

Icon(图标)

The window icon appears in the title bar and task bar icon on Windows and Linux, and in the dock icon on Mac OS X. Dialog and tool windows do not necessarily show their icon.

出现在标题栏的窗口图标和任务栏图标在Windows和Linux,在dock图标在Mac OS x Windows对话框和工具不一定展示他们的偶像。

Windows, Mac OS X and the Linux window managers each have their own preferred icon sizes:

Windows、Mac OS X和Linux窗口管理器都有自己喜欢的图标大小:

Windows XP

A 16x16 icon for the title bar and task bar.

一个16 x16的标题栏图标和任务栏。

A 32x32 icon for the Alt+Tab switcher.

为32 x32图标Alt + Tab切换器。

Mac OS X

Any number of icons of resolutions 16x16, 24x24, 32x32, 48x48, 72x72 and 128x128. The actual image displayed will be interpolated to the correct size from those provided.

任意数量的决议16 x16的象征,24 x24, 32 x32 48 x48, 72 x72和128 x128。实际的图像将显示插入到正确的大小的。

Linux

No constraints, however most window managers will use a 16x16 and a 32x32 icon in the same way as Windows XP.

没有约束,然而大多数窗口管理器将使用一个16 x16和32 x32图标一样Windows XP。

The set_icon() method allows you to set any number of images as the icon. pyglet will select the most appropriate ones to use and apply them to the window. If an alternate size is required but not provided, pyglet will scale the image to the correct size using a simple interpolation algorithm.

set_icon()方法允许您设置任意数量的图片图标。pyglet将选择最合适的使用和应用的窗口。如果需要另一个大小但不提供,pyglet将尺度图像的正确大小使用一个简单的插值算法。

The following example provides both a 16x16 and a 32x32 image as the window icon:

下面的例子提供了一个16 x16和一个32 x32形象的窗口图标:

window = pyglet.window.Window()
icon1 = pyglet.image.load('16x16.png')
icon2 = pyglet.image.load('32x32.png')
window.set_icon(icon1, icon2)

You can use images in any format supported by pyglet, however it is recommended to use a format that supports alpha transparency such as PNG. Windows .ico files are supported only on Windows, so their use is discouraged. Mac OS X .icons files are not supported at all.

您可以使用图像pyglet支持的任何格式,但是建议使用PNG等格式,它支持alpha透明度。支持Windows ico文件只在Windows上,所以它们的使用是气馁。不支持Mac OS X .icons文件。

Note that the icon that you set at runtime need not have anything to do with the application icon, which must be encoded specially in the application binary (see Self-contained executables).

注意,您设置的图标在运行时不需要与应用程序图标,必须专门在应用程序二进制编码(参见独立的可执行文件)。

Visibility(可见性)

Windows have several states of visibility. Already shown is the visible property which shows or hides the window.

窗户有几个州的可见性。已经证明是可见的属性显示或隐藏的窗口。

Windows can be minimized, which is equivalent to hiding them except that they still appear on the taskbar (or are minimised to the dock, on OS X). The user can minimize a window by clicking the appropriate button in the title bar. You can also programmatically minimize a window using the minimize method (there is also a corresponding maximize method).

Windows可以最小化,相当于隐藏他们除了他们仍然出现在任务栏(或最小化到码头,OS X)。用户可以最小化窗口的标题栏中点击相应的按钮。您也可以通过编程方式使用最小化方法最小化窗口(也有相应的最大化方法)。

When a window is made visible the on_show() event is triggered. When it is hidden the on_hide() event is triggered. On Windows and Linux these events will only occur when you manually change the visibility of the window or when the window is minimized or restored. On Mac OS X the user can also hide or show the window (affecting visibility) using the Command+H shortcut.

当一个窗口可见on_show()事件触发。当它是隐藏on_hide()事件触发。在Windows和Linux这些事件只会发生在当你手动改变窗口的可见性,当窗口被最小化或者恢复。在Mac OS X用户也可以隐藏或显示窗口(影响能见度)使用命令+ H捷径。

Subclassing Window(窗口子类化)

A useful pattern in pyglet is to subclass Window for each type of window you will display, or as your main application class. There are several benefits:

pyglet中一个有用的模式是为每个类型的子类窗口将显示窗口,或作为主要的应用程序类。有几个好处:

  • You can load font and other resources from the constructor, ensuring the OpenGL context has already been created.

你可以从构造函数加载字体和其他资源,确保OpenGL环境已经创建。

    • You can add event handlers simply be defining them on the class. The on_resize() event will be called as soon as the window is created (this doesn’t usually happen, as you must create the window before you can attach event handlers).

您可以添加事件处理程序只是在类定义它们。on_resize()事件将被称为当窗口被创建(这通常不会发生,因为您必须创建窗口之前您可以附加事件处理程序)。

  • There is reduced need for global variables, as you can maintain application state on the window.

减少需要全局变量,您可以维护应用程序状态的窗口。

The following example shows the same “Hello World” application as presented in Writing a pyglet application, using a subclass of Window:

下面的例子显示了相同的“Hello World”应用程序写pyglet展示的应用程序,使用一个子类窗口:

class HelloWorldWindow(pyglet.window.Window):
    def __init__(self):
        super(HelloWorldWindow, self).__init__()

        self.label = pyglet.text.Label('Hello, world!')

    def on_draw(self):
        self.clear()
        self.label.draw()

if __name__ == '__main__':
    window = HelloWorldWindow()
    pyglet.app.run()

This example program is located in examples/programming_guide/window_subclass.py.

这个示例程序位于/ programming_guide / window_subclass.py例子。

Windows and OpenGL contexts(Windows和OpenGL环境)

Every window in pyglet has an associated OpenGL context. Specifying the configuration of this context has already been covered in Creating a window. Drawing into the OpenGL context is the only way to draw into the window’s client area.

pyglet每个窗口都有一个关联的OpenGL上下文。指定的配置这种背景下已经在创建一个窗口覆盖。画在OpenGL环境的唯一方法画进窗口的客户区。

Double-buffering(双缓冲)

If the window is double-buffered (i.e., the configuration specified double_buffer=True, the default), OpenGL commands are applied to a hidden back buffer. This back buffer can be copied to the window using the <cite>flip</cite> method. If you are using the standard <cite>pyglet.app.run</cite> or pyglet.app.EventLoop event loop, this is taken care of automatically after each on_draw() event.

如果窗口功能(即。,配置指定double_buffer = True,默认),OpenGL命令应用于一个隐藏的缓冲区。这个缓冲区可以复制到窗口使用翻转方法。如果您使用的是标准pyglet.app.run或pyglet.app。EventLoop事件循环,这是后自动照顾每个on_draw()事件。

If the window is not double-buffered, the flip() operation is unnecessary, and you should remember only to callpyglet.gl.glFlush() to ensure buffered commands are executed.

如果窗口没有功能,翻转()操作是不必要的,你只要记得打电话给pyglet.gl.glFlush(),以确保缓冲执行命令。

Vertical retrace synchronisation(垂直同步)

Double-buffering eliminates one cause of flickering: the user is unable to see the image as it painted, only the final rendering. However, it does introduce another source of flicker known as “tearing”.

双缓冲消除闪烁的一个原因:用户无法看到它画的形象,只有最终渲染。然而,它确实引入了另一个闪烁的来源被称为“撕裂”。

Tearing becomes apparent when displaying fast-moving objects in an animation. The buffer flip occurs while the video display is still reading data from the framebuffer, causing the top half of the display to show the previous frame while the bottom half shows the updated frame. If you are updating the framebuffer particularly quickly you may notice three or more such “tears” in the display.

撕裂,显示快速运动的物体,一个动画。缓冲区翻转发生在视频显示仍从framebuffer读取数据,导致显示的上半部分显示前一帧,而下半部分显示了更新后的框架。如果你更新framebuffer尤其是很快你会发现三个或更多这样的“眼泪”显示。

pyglet provides a way to avoid tearing by synchronising buffer flips to the video refresh rate. This is enabled by default, but can be set or unset manually at any time with the vsync (vertical retrace synchronisation) property. A window is created with vsync initially disabled in the following example:

pyglet提供了一种方法来避免撕裂同步缓冲区翻转视频刷新率。这是默认启用,但设置手动在任何时候可以与vsync(垂直追溯同步)属性。创建一个窗口与vsync最初残疾人在以下例子:

window = pyglet.window.Window(vsync=False)

It is usually desirable to leave vsync enabled, as it results in flicker-free animation. There are some use-cases where you may want to disable it, for example:

是最理想离开vsync启用,因为它导致无闪烁的动画。在一些用例中,您可能想要禁用它,例如:

  • Profiling an application. Measuring the time taken to perform an operation will be affected by the time spent waiting for the video device to refresh, which can throw off results. You should disable vsync if you are measuring the performance of your application.

分析应用程序。测量的时间来执行一个操作会受到时间的影响,等待更新的视频设备,可以抛弃的结果。你应该禁用vsync测量应用程序的性能。

  • If you cannot afford for your application to block. If your application run loop needs to quickly poll a hardware device, for example, you may want to avoid blocking with vsync.

如果你不能为您的应用程序阻塞。如果您的应用程序运行循环需要迅速调查一个硬件设备,例如,您可能希望避免与vsync阻塞。

Note that some older video cards do not support the required extensions to implement vsync; this will appear as a warning on the console but is otherwise ignored.

注意,一些老的显卡不支持所需的扩展来实现vsync;这将会出现一个警告在控制台上,但忽略了。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,590评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 86,808评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,151评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,779评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,773评论 5 367
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,656评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,022评论 3 398
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,678评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 41,038评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,659评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,756评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,411评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,005评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,973评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,203评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,053评论 2 350
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,495评论 2 343

推荐阅读更多精彩内容