how to make a text input box python pygame

Solutions on MaxInterview for how to make a text input box python pygame by the best coders in the world

showing results for - "how to make a text input box python pygame"
Valeria
27 Jul 2018
1import pygame as pg
2
3
4pg.init()
5screen = pg.display.set_mode((640, 480))
6COLOR_INACTIVE = pg.Color('lightskyblue3')
7COLOR_ACTIVE = pg.Color('dodgerblue2')
8FONT = pg.font.Font(None, 32)
9
10
11class InputBox:
12
13    def __init__(self, x, y, w, h, text=''):
14        self.rect = pg.Rect(x, y, w, h)
15        self.color = COLOR_INACTIVE
16        self.text = text
17        self.txt_surface = FONT.render(text, True, self.color)
18        self.active = False
19
20    def handle_event(self, event):
21        if event.type == pg.MOUSEBUTTONDOWN:
22            # If the user clicked on the input_box rect.
23            if self.rect.collidepoint(event.pos):
24                # Toggle the active variable.
25                self.active = not self.active
26            else:
27                self.active = False
28            # Change the current color of the input box.
29            self.color = COLOR_ACTIVE if self.active else COLOR_INACTIVE
30        if event.type == pg.KEYDOWN:
31            if self.active:
32                if event.key == pg.K_RETURN:
33                    print(self.text)
34                    self.text = ''
35                elif event.key == pg.K_BACKSPACE:
36                    self.text = self.text[:-1]
37                else:
38                    self.text += event.unicode
39                # Re-render the text.
40                self.txt_surface = FONT.render(self.text, True, self.color)
41
42    def update(self):
43        # Resize the box if the text is too long.
44        width = max(200, self.txt_surface.get_width()+10)
45        self.rect.w = width
46
47    def draw(self, screen):
48        # Blit the text.
49        screen.blit(self.txt_surface, (self.rect.x+5, self.rect.y+5))
50        # Blit the rect.
51        pg.draw.rect(screen, self.color, self.rect, 2)
52
53
54
55def main():
56    clock = pg.time.Clock()
57    input_box1 = InputBox(100, 100, 140, 32)
58    input_box2 = InputBox(100, 300, 140, 32)
59    input_boxes = [input_box1, input_box2]
60    done = False
61
62    while not done:
63        for event in pg.event.get():
64            if event.type == pg.QUIT:
65                done = True
66            for box in input_boxes:
67                box.handle_event(event)
68
69        for box in input_boxes:
70            box.update()
71
72        screen.fill((30, 30, 30))
73        for box in input_boxes:
74            box.draw(screen)
75
76        pg.display.flip()
77        clock.tick(30)
78
79
80if __name__ == '__main__':
81    main()
82    pg.quit()
queries leading to this page
python pygame tutorial getting text inputhow to make imputs show up in a rect pygamea board game and input text box in pygamehow to create a input box in pygamepygame input fieldspygame input boxpython pygame text box inputpygame get text inputpygame user input texthow to make input in pygamehow to create an input box in pygamehow to make a input box in pygamehow to make a text box in pygamehow to make an input box in pygameinput text in pygamehow to keep a number input box in pygamecan you input in pygamehow to input text in pygamedisplay input text in pygametkinter text box pre textwrite player in box input in pygamepygame input tutorialpygame textinputhow to create box inputs in pygamepygame text boztextbox pygameinput box code pygameinput pygameinputbox pygameinput box pygameinput box in pygamepygame add input prompthow to make text field pygametext box input pygamepygame textboxpygame gui textboxenter in pygame texthow to make a text input box python pygamehow to add input text box in pygamehow to maek a text box in oygameinput text pygamegetting text input pygame pythonhow to get a text box in pygamecode to display text with input in pygamehow to draw text box pygamehow to make a textbox in pygamehow to make text input in pygamehow to make inputs in pygamehow to create a text box in python pygamepygame how to make inputpygame text inputpygame text fieldcreate input input pygamehow can i create a text input box with pygamepython pygame input boxpygame gui user text inputtexbox python pygametext input in pygamepygame input textetext input box pygamepygame input textget text input pygamepygame getting text inputinput for pygamehow to get text input with pygamehow to draw a text input pygamepygame text input fieldstext input pygamepygame text boxcode to make a text box with input in pygamepygame input fieldpygame inputinput button in pygamepygame iteractive textpygame input text boxusing pygame for inputhowt put input in pygametextbox in pygameinput in pygamepygame enter texthow to make a text input box python pygame