pygame text input
1.0.0
이 모듈은 Pygame을 사용하여 텍스트 입력을 단순화하는 두 가지 유틸리티 클래스를 제공합니다. 수업은 다음과 같습니다.
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 렌더링에 사용되는 개체 |
| 안티 리아 | 글꼴을 antialiased 렌더링 할 것인지 여부 |
| 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 )