How to create a text input box with Pygame? Last Updated : 26 Mar, 2021 Comments Improve Suggest changes 15 Likes Like Report In this article, we will discuss how to create a text input box using PyGame. Installation Before initializing pygame library we need to install it. This library can be installed into the system by using pip tool that is provided by Python for its library installation. Pygame can be installed by writing these lines into the terminal. We can install Pygame using command : pip install pygame Pygame can be used to create a text input box which will be explained step by step further in this article. ApproachUse pygame.init() which will initialize all imported modules.Set screen size.Set font of the text which user will type.Create a condition according to user key.Also, declare two variable which will contain color name which will be further used for input color.Also, store input in a variable to display on screen.Now draw rectangle and pass argument which should be on screen.Also, set the size of screen to be rendered.Use clock.tick() which means that for every second at most given frames should be passed.Functions UsedFunctionDescriptionclock.tick()It is used to refresh the frame in given secondpygame.exit()It is used to quit gamepygame.init()It is used to initialize all imported modulepygame.font.FontCreate a new Font object from a filepygame.display.flip()It will update only a portion of the screen to updated, not full areascreen.fill((r, g, b, a))It will set the background color of the screen. The range is between 0 and 255.Implementation Python3 # import sys module import pygame import sys # pygame.init() will initialize all # imported module pygame.init() clock = pygame.time.Clock() # it will display on screen screen = pygame.display.set_mode([600, 500]) # basic font for user typed base_font = pygame.font.Font(None, 32) user_text = '' # create rectangle input_rect = pygame.Rect(200, 200, 140, 32) # color_active stores color(lightskyblue3) which # gets active when input box is clicked by user color_active = pygame.Color('lightskyblue3') # color_passive store color(chartreuse4) which is # color of input box. color_passive = pygame.Color('chartreuse4') color = color_passive active = False while True: for event in pygame.event.get(): # if user types QUIT then the screen will close if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.MOUSEBUTTONDOWN: if input_rect.collidepoint(event.pos): active = True else: active = False if event.type == pygame.KEYDOWN: # Check for backspace if event.key == pygame.K_BACKSPACE: # get text input from 0 to -1 i.e. end. user_text = user_text[:-1] # Unicode standard is used for string # formation else: user_text += event.unicode # it will set background color of screen screen.fill((255, 255, 255)) if active: color = color_active else: color = color_passive # draw rectangle and argument passed which should # be on screen pygame.draw.rect(screen, color, input_rect) text_surface = base_font.render(user_text, True, (255, 255, 255)) # render at position stated in arguments screen.blit(text_surface, (input_rect.x+5, input_rect.y+5)) # set width of textfield so that text cannot get # outside of user's text input input_rect.w = max(100, text_surface.get_width()+10) # display.flip() will update only a portion of the # screen to updated, not full area pygame.display.flip() # clock.tick(60) means that for every second at most # 60 frames should be passed. clock.tick(60) Output: Create Quiz Comment C chetanjha888 Follow 15 Improve C chetanjha888 Follow 15 Improve Article Tags : Python Python-PyGame Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 4 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 3 min read Python MySQL 9 min read Python Packages 10 min read Python Modules 3 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 4 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 3 min read StatsModel Library - Tutorial 3 min read Learning Model Building in Scikit-learn 6 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 5 min read Build a REST API using Flask - Python 3 min read Building a Simple API with Django REST Framework 3 min read Python PracticePython Quiz 1 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like