โมดูลนี้มีคลาสยูทิลิตี้สองคลาสที่ทำให้การป้อนข้อความง่ายขึ้นโดยใช้ pygame ชั้นเรียนคือ:
TextInputVisualizer ซึ่งสามารถใช้ในการจัดการและวาดอินพุตข้อความ เพียงแค่ส่งเหตุการณ์ทั้งหมดที่ส่งคืนโดย pygame.event.get() ไปยังทุกเฟรมและทำให้แอตทริบิวต์ surface ของมันบนหน้าจอTextInputManager ที่สามารถใช้ในการจัดการข้อความที่ป้อนเข้าโดยไม่มีแง่มุมภาพ ใช้โดย TextInputVisualizer เบื้องหลังวิธีที่ง่ายที่สุดคือการใช้ PYPI:
python3 -m pip install pygame-textinput
TextInputVisualizer อาร์กิวเมนต์ทั้งหมดไปยังตัวสร้างเป็นทางเลือก เมื่อสร้างขึ้นแล้วให้ update ทุกเฟรมด้วยเหตุการณ์ pygame ทั้งหมดเป็นอาร์กิวเมนต์จากนั้น blit ฟิลด์ 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 | (บูล) ในขณะนี้เคอร์เซอร์สามารถมองเห็นได้หรือไม่ พลิกทุก cursor_interval MS ตราบใดที่ update เรียกอย่างต่อเนื่อง |
| วิธี | คำอธิบาย |
|---|---|
| อัปเดต (กิจกรรม) | เรียกใช้วิธีนี้ทุกเฟรมตราบเท่าที่ควรประมวลผลอินพุตและ 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() ซึ่งคุณต้องการให้ประมวลผล TextInputVisualizer ทำสิ่งนี้ให้คุณในวิธี update หากคุณผ่าน TextInputManager
| การโต้แย้ง | คำอธิบาย |
|---|---|
| อักษรย่อ | ค่าเริ่มต้น (ข้อความ) |
| ผู้ตรวจสอบความถูกต้อง | ฟังก์ชั่นการใช้ 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 )