この命令は試験的に実装されているものです!:pygameのバージョンが上がると、この命令は仕様が変更されたりなくなったりする可能性があります。この項目にある命令を使用してプログラムを書くと、次期pygameバージョンでは正常に動かなくなることもあります。
このコピー・ペースト命令では、クリップボートにコピーしたりクリップボード内のデータを貼り付けたりすることができます。pygameで作られたアプリケーション同士だけでなく、他のアプリケーションとの間でもコピー・ペーストができます。この命令では下記のようないくつかの既定データ形式が定義されています。
SCRAP_PPM SCRAP_PBM SCRAP_BMP SCRAP_TEXT
これらを設定することで、pygameに既定に定義されているデータ形式をクリップボードで使用できるようになります。SCRAP_PPMやSCRAP_PBMはSCRAP_BMPは他のアプリケーションに画像データをコピーするのに適しており、一方 SCRAP_TEXTは文字のコピーに適しています。
SCRAP_*の各定義を設定すると、下記のMIMEタイプを参照して、指定したデータ形式を処理するための既定オペレーティングシステムの設定も行われます。:
SCRAP_TEXT text/plain # プレーンテキスト SCRAP_PBM image/pbm # PBMでエンコードされた画像ファイル SCRAP_PPM image/ppm # PPMでエンコードされた画像ファイル SCRAP_BMP image/bmp # BMPでエンコードされた画像ファイル
使用環境にもよりますが、クリップボードにコピーしたデータが保存されている場合は、処理するデータ形式が自動的に追加登録されることもあります。これは他のアプリケーションとのコピー・ペースト連携を正常に行うためです。 pygame.scrapモジュールの命令に文字列をコピーすると、後で挙げる文字コードを使用することができます。
Windows環境では、処理するデータ形式が自動的に追加登録されます。各データ形式はpygame内で下記のように定義されています。:
text/plain;charset=utf-8 # UTF-8 でエンコードされた文字 audio/wav # WAVでエンコードされた音声ファイル image/tiff # TIFFでエンコードされた画像ファイル
X11環境では、処理するデータ形式が自動的に追加登録されます。各データ形式はpygame内で下記のように定義されています。:
UTF8_STRING # UTF-8 でエンコードされた文字 text/plain;charset=utf-8 # UTF-8 でエンコードされた文字 COMPOUND_TEXT # コンパウンドテキスト
先ほど述べたようにクリップボードには独自のデータ形式を定義して使用することもできますが、その場合他のアプリケーションとコピー・ペースト連携ができない場合もあります。つまり、下記のような操作でクリップボードにデータをコピーしたとします。
pygame.scrap.put ("own_data", data)
そうすると、このコピーデータは"own_data"というデータ形式定義を持つアプリケーションでしか使用することができません。
この命令はpygameのバージョン1.8か新しく実装されました。今ところWindows環境と X11環境、Mac OS X環境でしか動作しません。Mac OSX環境では、今のところ文字のコピー・ペーストしか実行できません。 - 他のデータに関しては次期バージョンでサポートする予定です。
EXPERIMENTAL!: meaning this api may change, or dissapear in later pygame releases. If you use this, your code will break with the next pygame release.
The scrap module is for getting and putting stuff from the clipboard. So you can copy and paste things between pygame, and other application types. It defines some basic own data types
SCRAP_PPM SCRAP_PBM SCRAP_BMP SCRAP_TEXT
to be placed into the clipboard and allows to use define own clipboard types. SCRAP_PPM, SCRAP_PBM and SCRAP_BMP are suitable for surface buffers to be shared with other applications, while SCRAP_TEXT is an alias for the plain text clipboard type.
The SCRAP_* types refer to the following MIME types and register those as well as the default operating system type for this type of data:
SCRAP_TEXT text/plain for plain text SCRAP_PBM image/pbm for PBM encoded image data SCRAP_PPM image/ppm for PPM encoded image data SCRAP_BMP image/bmp for BMP encoded image data
Depending on the platform additional types are automatically registered when data is placed into the clipboard to guarantee a consistent sharing behaviour with other applications. The following listed types can be used as string to be passed to the respective pygame.scrap module functions.
For Windows platforms, the additional types are supported automatically and resolve to their internal definitions:
text/plain;charset=utf-8 for UTF-8 encoded text audio/wav for WAV encoded audio image/tiff for TIFF encoded image data
For X11 platforms, the additional types are supported automatically and resolve to their internal definitions:
UTF8_STRING for UTF-8 encoded text text/plain;charset=utf-8 for UTF-8 encoded text COMPOUND_TEXT for COMPOUND text
As stated before you can define own types for the clipboard, those however might not be usable by other applications. Thus data pasted into the clipboard using
pygame.scrap.put ("own_data", data)
can only be used by applications, which query the clipboard for the "own_data" type.
New in pygame 1.8. Only works for Windows, X11 and Mac OS X so far. On Mac OSX only text works at the moment - other types will be supported in the next release.
scrapモジュールを初期化し、初期化に失敗した場合は例外を発生させます。 このモジュールを使用するにはdisplay surfaceの設定が行われている必要があるので、事前に必ずpygame.display.set_mode命令を実行するようにしてください。
Tries to initialize the scrap module and raises an exception, if it fails. Note that this module requires a set display surface, so you have to make sure, you acquired one earlier using pygame.display.set_mode.
クリップボードから、指定した形式のデータを戻り値として返します。データは文字列として取得されるので、別途変換処理が必要な場合もあります。もし指定した形式のデータがない場合は、None値が戻り値として返ります。
text = pygame.scrap.get (SCRAP_TEXT)
if text:
# 取得したデータを使った処理を記述して行ってください。
else:
print "There does not seem to be text in the clipboard."
Returns the data for the specified type from the clipboard. The data is returned as string and might need further processing. If no data for the passed type is available, None is returned.
text = pygame.scrap.get (SCRAP_TEXT)
if text:
# Do stuff with it.
else:
print "There does not seem to be text in the clipboard."
クリップボードに保存されたデータのデータ形式一覧を、文字列の識別子として取得します。取得された各識別子は、 scrap.get()命令でクリップボードから特定の形式のデータを取り出す時に使用します。クリップボードにデータが保存されてない場合、空のリストが戻り値として返ります。
types = pygame.scrap.get_types ()
for t in types:
if "text" in t:
# クリップボードに"テキスト"形式のデータが保存されています。
# テキスト形式なのでprint命令で使用できます。
print pygame.scrap.get (t)
Gets a list of strings with the identifiers for the available clipboard types. Each identifier can be used in the scrap.get() method to get the clipboard content of the specific type. If there is no data in the clipboard, an empty list is returned.
types = pygame.scrap.get_types ()
for t in types:
if "text" in t:
# There is some content with the word "text" in it. It's
# possibly text, so print it.
print pygame.scrap.get (t)
クリップボードに指定した形式のデータを保存します。 The data must be a string buffer. type引数には、クリップボードに保存するデータ形式を文字列型識別子で設定します。文字列型識別子として設定できるのは、既定で定義されているSCRAP_PBMや SCRAP_PPM、SCRAP_BMP、SCRAP_TEXTに加えてあなたが独自に定義した識別子です。
クリップボードのデータが保存できない場合、例外が発生します。
fp = open ("example.bmp", "rb")
pygame.scrap.put (SCRAP_BMP, fp.read ())
fp.close ()
# これで他のアプリケーションでもクリップボードに保存された画像データを使用できます。
pygame.scrap.put (SCRAP_TEXT, "A text to copy")
pygame.scrap.put ("Plain text", "A text to copy")
Places data for a specific clipboard type into the clipboard. The data must be a string buffer. The type is a string identifying the type of data placed into the clipboard. This can be one of the native SCRAP_PBM, SCRAP_PPM, SCRAP_BMP or SCRAP_TEXT values or an own string identifier.
The method raises an exception, if the content could not be placed into the clipboard.
fp = open ("example.bmp", "rb")
pygame.scrap.put (SCRAP_BMP, fp.read ())
fp.close ()
# Now you can acquire the image data from the clipboard in other
# applications.
pygame.scrap.put (SCRAP_TEXT, "A text to copy")
pygame.scrap.put ("Plain text", "A text to copy")
指定した形式のデータがクリップボードで使用できる場合はTrueが戻り値として返り、そうでない場合はFalseが戻り値として返ります。
if pygame.scrap.contains (SCRAP_TEXT):
print "テキスト形式のデータはクリップボードで使用できます。"
if pygame.scrap.contains ("own_data_type"):
print "この形式のデータはクリップボードで使用できます。"
Returns True, if data for the passed type is available in the clipboard, False otherwise.
if pygame.scrap.contains (SCRAP_TEXT):
print "There is text in the clipboard."
if pygame.scrap.contains ("own_data_type"):
print "There is stuff in the clipboard."
このpygameアプリケーションがクリップボード機能を排他制御している場合はFalseが戻り値として返り、そうでない場合はTrueが戻り値として返ります。
if pygame.scrap.lost ():
print "このアプリケーションで保存しているデータはありません。クリップボードは別のアプリケーションが排他制御しています。"
※原文では「排他制御している場合はTrueが戻り値として返り、そうでない場合はFalseが戻り値として返ります。」と記載されていますが、誤りの可能性があるので修正して訳し直しています。
Returns True, if the clipboard is currently owned by the pygame application, False otherwise.
if pygame.scrap.lost ():
print "No content from me anymore. The clipboard is used by someone else."
クリップボードのアクセスモードを設定します。これはX11環境のためだけに用意された命令で、mouse selectionsモード (SCRAP_SELECTION) と the clipboard モード(SCRAP_CLIPBOARD) が使用できます。X11以外の環境ではSCRAP_SELECTIONモードを設定しても特に何も起こりません。
SCRAP_CLIPBOARDやSCRAP_SELECTION以外の値を引数に設定すると、ValueErrorの例外が発生します。
Sets the access mode for the clipboard. This is only of interest for X11 environments, where clipboard modes for mouse selections (SRAP_SELECTION) and the clipboard (SCRAP_CLIPBOARD) are available. Setting the mode to SCRAP_SELECTION in other environments will not cause any difference.
If a value different from SCRAP_CLIPBOARD or SCRAP_SELECTION is passed, a ValueError will be raised.