pygame.scrap
pygame module for clipboard support.
Initializes the scrap module.
Returns True if the scrap module is currently initialized.
Gets the data for the specified type from the clipboard.
Gets a list of the available clipboard types.
Places data into the clipboard.
Checks whether data for a given type is available in the clipboard.
Indicates if the clipboard ownership has been lost by the pygame application.
Sets the clipboard access mode.

实验性的! :该接口可能会在以后的pyGame版本中更改或消失。如果您使用它,您的代码可能会与下一个pyGame发行版发生冲突。

报废模块用于将数据传输到剪贴板或从剪贴板传输数据。这允许在pyGame和其他应用程序之间剪切和粘贴数据。定义和注册了一些基本数据(MIME)类型:

 pygame         string
constant        value        description
--------------------------------------------------
SCRAP_TEXT   "text/plain"    plain text
SCRAP_BMP    "image/bmp"     BMP encoded image data
SCRAP_PBM    "image/pbm"     PBM encoded image data
SCRAP_PPM    "image/ppm"     PPM encoded image data

pygame.SCRAP_PPMpygame.SCRAP_PBMpygame.SCRAP_BMP 适用于与其他应用程序共享的表面缓冲区。 pygame.SCRAP_TEXT 是纯文本剪贴板类型的别名。

根据平台的不同,当数据被放入剪贴板时,其他类型会自动注册,以保证与其他应用程序的一致共享行为。下面列出的类型可以用作要传递给各自 pygame.scrappygame module for clipboard support. 模块功能。

窗口 平台,则自动支持这些附加类型,并将其解析为其内部定义:

"text/plain;charset=utf-8"   UTF-8 encoded text
"audio/wav"                  WAV encoded audio
"image/tiff"                 TIFF encoded image data

X11 平台,则自动支持这些附加类型,并将其解析为其内部定义:

"text/plain;charset=utf-8"   UTF-8 encoded text
"UTF8_STRING"                UTF-8 encoded text
"COMPOUND_TEXT"              COMPOUND text

可以使用用户定义的类型,但数据可能无法由其他应用程序访问,除非它们知道要查找的数据类型。示例:由放置到剪贴板中的数据 pygame.scrap.put("my_data_type", byte_data) 只能由在剪贴板中查询 "my_data_type" 数据类型。

有关报废模块如何工作的示例,请参阅示例页面 (pygame.examples.scrap_clipboard.main()access the clipboard )或直接在GitHub中编写代码 (pygame/examples/scrap_clipboard.py )。

New in pygame 1.8.

备注

报废模块目前只支持Windows、X11和Mac OS X。在Mac OS X上目前只支持文本-其他类型的模块可能会在未来的版本中支持。

pygame.scrap.init()
Initializes the scrap module.
init() -> None

对报废模块进行初始化。

引发

pygame.errorstandard pygame exception -- 如果无法初始化报废模块

备注

报废模块需要 pygame.display.set_mode()Initialize a window or screen for display 在初始化之前被调用。

pygame.scrap.get_init()
Returns True if the scrap module is currently initialized.
get_init() -> bool

获取报废模块的初始化状态。

返回

True 如果 pygame.scrappygame module for clipboard support. 模块当前已初始化, False 否则

返回类型

bool

New in pygame 1.9.5.

pygame.scrap.get()
Gets the data for the specified type from the clipboard.
get(type) -> bytes | None

从剪贴板中检索指定类型的数据。数据以字节字符串的形式返回,可能需要进一步处理(例如解码为Unicode)。

参数

type (string) -- 要从剪贴板检索的数据类型

返回

给定类型标识符的数据(字节对象)或 None 如果没有给定类型的数据可用

返回类型

bytes | None

text = pygame.scrap.get(pygame.SCRAP_TEXT)
if text:
    print("There is text in the clipboard.")
else:
    print("There does not seem to be text in the clipboard.")
pygame.scrap.get_types()
Gets a list of the available clipboard types.
get_types() -> list

获取剪贴板上当前可用的数据的数据类型字符串标识符列表。每个标识符都可以在 pygame.scrap.get()Gets the data for the specified type from the clipboard. 方法以获取特定类型的剪贴板内容。

返回

可用剪贴板数据类型的字符串列表,如果剪贴板中没有数据,则返回空列表

返回类型

list

for t in pygame.scrap.get_types():
    if "text" in t:
        # There is some content with the word "text" in its type string.
        print(pygame.scrap.get(t))
pygame.scrap.put()
Places data into the clipboard.
put(type, data) -> None

将给定剪贴板类型的数据放入剪贴板。数据必须是字符串缓冲区。类型是标识要放入剪贴板的数据类型的字符串。这可以是预定义的 pygame.SCRAP_PBMpygame.SCRAP_PPMpygame.SCRAP_BMPpygame.SCRAP_TEXT 值或用户定义的字符串标识符。

参数
  • type (string) -- 要放入剪贴板的数据的类型标识符

  • data (bytes) -- 要放入剪贴板的数据,字节对象

引发

pygame.errorstandard pygame exception -- 如果无法将数据放入剪贴板

with open("example.bmp", "rb") as fp:
    pygame.scrap.put(pygame.SCRAP_BMP, fp.read())
# The image data is now on the clipboard for other applications to access
# it.
pygame.scrap.put(pygame.SCRAP_TEXT, b"A text to copy")
pygame.scrap.put("Plain text", b"Data for user defined type 'Plain text'")
pygame.scrap.contains()
Checks whether data for a given type is available in the clipboard.
contains(type) -> bool

检查剪贴板中当前是否有给定类型的数据。

参数

type (string) -- 要检查其可用性的数据类型

返回

True 如果剪贴板中有已传递类型的数据, False 否则

返回类型

bool

if pygame.scrap.contains(pygame.SCRAP_TEXT):
    print("There is text in the clipboard.")
if pygame.scrap.contains("own_data_type"):
    print("There is stuff in the clipboard.")
pygame.scrap.lost()
Indicates if the clipboard ownership has been lost by the pygame application.
lost() -> bool

指示剪贴板所有权是否已被pyGame应用程序丢失。

返回

True 如果剪贴板所有权已被PYGAME应用程序丢失, False 如果pyGame应用程序仍然拥有剪贴板

返回类型

bool

if pygame.scrap.lost():
    print("The clipboard is in use by another application.")
pygame.scrap.set_mode()
Sets the clipboard access mode.
set_mode(mode) -> None

设置剪贴板的访问模式。这仅适用于剪贴板模式的X11环境 pygame.SCRAP_SELECTION (用于鼠标选择)和 pygame.SCRAP_CLIPBOARD (用于剪贴板)可用。将模式设置为 pygame.SCRAP_SELECTION 在其他环境中不会将模式从 pygame.SCRAP_CLIPBOARD

参数

mode -- access mode, supported values are pygame.SCRAP_CLIPBOARD and pygame.SCRAP_SELECTION (pygame.SCRAP_SELECTION only has an effect when used on X11 platforms)

引发

ValueError -- 如果 mode 参数不是 pygame.SCRAP_CLIPBOARDpygame.SCRAP_SELECTION




Edit on GitHub