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對像用於渲染 |
| 反式 | 是否渲染字體 |
| 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 | (布爾)光標目前是否可見。只要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一樣,您可以為其update方法餵食pygame.event.get()收到的所有事件,您希望它能處理。如果您將其傳遞給TextInputManager ,則TextInputVisualizer在其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 )