pygame text input
1.0.0
このモジュールは、pygameを使用してテキストの入力を簡素化する2つのユーティリティクラスを提供します。クラスは次のとおりです。
TextInputVisualizer 。 pygame.event.get()によって返されたすべてのイベントをすべてのフレームに渡し、画面上のsurface属性を促進するだけです。TextInputManager 。舞台裏でTextInputVisualizerによって使用されます。最も簡単な方法は、Pypiを使用することです。
python3 -m pip install pygame-textinput
TextInputVisualizerコンストラクターに対するすべての引数はオプションです。構築されたら、すべてのPygameイベントを引数としてupdateすべてのフレームを呼び出してから、画面にsurfaceフィールドをブリットします。
import pygame_textinput
import pygame
pygame . init ()
# Create TextInput-object
textinput = pygame_textinput . TextInputVisualizer ()
screen = pygame . display . set_mode (( 1000 , 200 ))
clock = pygame . time . Clock ()
while True :
screen . fill (( 225 , 225 , 225 ))
events = pygame . event . get ()
# Feed it with events every frame
textinput . update ( events )
# Blit its surface onto the screen
screen . blit ( textinput . surface , ( 10 , 10 ))
for event in events :
if event . type == pygame . QUIT :
exit ()
pygame . display . update ()
clock . tick ( 30 )すべての引数はオプションです。
| 口論 | 説明 |
|---|---|
| マネージャー | TextInputManagerは、入力の管理に使用されていました |
| font_object | レンダリングに使用されるpygame.font.Fontオブジェクト |
| アンチアリアス | フォントをアンチアライアス化するかどうか |
| font_color | フォントの色がレンダリングされました |
| cursor_blink_interval | MSのカーソル点滅の間隔 |
| cursor_width | ピクセル単位のカーソルの幅 |
| cursor_color | カーソルの色 |
上記のすべての引数は、このような場所でアクセスして変更できるフィールドでもあります。
textinput . cursor_width = 12
textinput . value = "Hello, World!"
print ( textinput . font_color )| 分野 | 説明 |
|---|---|
| 価値 | (文字列)これまでに入力されたテキスト |
| 表面 | 入力されたテキストとカーソルを備えたpygame.Surfaceオブジェクトと透明な背景。設定できません。 |
| cursor_visible | (bool)カーソルが現在見えるかどうかにかかわらず。 updateが継続的に呼ばれる限り、すべてのcursor_interval MSをフリップします。 |
| 方法 | 説明 |
|---|---|
| 更新(イベント) | 入力を処理し、 cursor_visibleを更新する必要がある限り、このメソッドをすべてのフレームに呼びます。 |
for event in events :
...
if event . type == pygame . KEYDOWN and event . key == pygame . K_RETURN :
print ( "Oooweee" )pygame内で直接有効にできるため、手動で導入されていません。 pygame . key . set_repeat ( 200 , 25 ) # press every 50 ms after waiting 200 msまた、この新しいバージョンは最適化されており、その場でフィールドを変更できるようになり、実際の表面はtextinput.surfaceを使用してアクセスした場合にのみ再レンダリングできます。
TextInputManager自分で画面にテキストを描画したい場合は、 TextInputManagerを使用して、これまでに入力された文字列のみを管理できます。
TextInputVisualizerと同様に、 pygame.event.get()が受信したすべてのイベントをupdate方法に送ります。 TextInputVisualizer 、 TextInputManagerを渡すと、 update方法内でこれを行います。
| 口論 | 説明 |
|---|---|
| イニシャル | 初期値(テキスト) |
| バリデーター | stringを取得してboolを返す関数。入力が値を変更するたびに、この関数は変更された値を引数として呼び出されます。関数がTrueを返すと、入力が受け入れられます。そうしないと、入力は無視されます。 |
だから、あなたは最大5文字までの入力のみを許可したいと言ってください、あなたはそれをすることができます
manager = TextInputManager ( validator = lambda input : len ( input ) <= 5 )| 分野 | 説明 |
|---|---|
| 価値 | これまで挿入された値。変更すると、 cursor_pos可能な限り保持されます。 |
| cursor_pos | カーソルの位置。 0最初のキャラクターの前であり、 len(manager.value)最後のポジションです。この範囲外の値は固定されています。 |
import pygame
import pygame . locals as pl
pygame . init ()
# No arguments needed to get started
textinput = TextInputVisualizer ()
# But more customization possible: Pass your own font object
font = pygame . font . SysFont ( "Consolas" , 55 )
# Create own manager with custom input validator
manager = TextInputManager ( validator = lambda input : len ( input ) <= 5 )
# Pass these to constructor
textinput_custom = TextInputVisualizer ( manager = manager , font_object = font )
# Customize much more
textinput_custom . cursor_width = 4
textinput_custom . cursor_blink_interval = 400 # blinking interval in ms
textinput_custom . antialias = False
textinput_custom . font_color = ( 0 , 85 , 170 )
screen = pygame . display . set_mode (( 1000 , 200 ))
clock = pygame . time . Clock ()
# Pygame now allows natively to enable key repeat:
pygame . key . set_repeat ( 200 , 25 )
while True :
screen . fill (( 225 , 225 , 225 ))
events = pygame . event . get ()
# Feed it with events every frame
textinput . update ( events )
textinput_custom . update ( events )
# Get its surface to blit onto the screen
screen . blit ( textinput . surface , ( 10 , 10 ))
screen . blit ( textinput_custom . surface , ( 10 , 50 ))
# Modify attributes on the fly - the surface is only rerendered when .surface is accessed & if values changed
textinput_custom . font_color = [( c + 10 ) % 255 for c in textinput_custom . font_color ]
# Check if user is exiting or pressed return
for event in events :
if event . type == pygame . QUIT :
exit ()
if event . type == pygame . KEYDOWN and event . key == pygame . K_RETURN :
print ( f"User pressed enter! Input so far: { textinput . value } " )
pygame . display . update ()
clock . tick ( 30 )