1

I have an input box class in Pygame, in which I only enter numbers and what I want, is that every time I press ENTER this value gets stored in a variable to be used from another function. Thank you all in advance. This is the input box code:

class InputBox:

    def __init__(self, x, y, w, h, text=''):
        self.rect = pg.Rect(x, y, w, h)
        self.color = COLOR_INACTIVE
        self.text = text
        self.txt_surface = FONT.render(text, True, self.color)
        self.active = False

    def handle_event(self, event):
        if event.type == pg.MOUSEBUTTONDOWN:
            # If the user clicked on the input_box rect.
            if self.rect.collidepoint(event.pos):
                self.active = not self.active
            else:
                self.active = False
            self.color = COLOR_ACTIVE if self.active else COLOR_INACTIVE
        if event.type == pg.KEYDOWN:
            if self.active:
                if event.key == pg.K_RETURN:
                    print(self.text)
                    self.text = ''
                elif event.key == pg.K_BACKSPACE:
                    self.text = self.text[:-1]
                else:
                    self.text += event.unicode
                self.txt_surface = FONT.render(self.text, True, self.color)

    def update(self):
        width = max(200, self.txt_surface.get_width()+10)
        self.rect.w = width

    def draw(self, screen):
        screen.blit(self.txt_surface, (self.rect.x+5, self.rect.y+5))
        pg.draw.rect(screen, self.color, self.rect, 2)


def main():
    ...

    while not done:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                done = True
            for box in input_boxes:
                box.handle_event(event)
                
        for box in input_boxes:
            box.update()

        screen.fill((30, 30, 30))
        for box in input_boxes:
            box.draw(screen)

        pg.display.flip()
        clock.tick(30)
Evaki
  • 23
  • 3

2 Answers2

0

Does this example help You understand the concept:

user_input = {'user_input': []}


class UserInput:
    def __init__(self):
        self.input_ = input('Input something: ')
        user_input['user_input'].append(self.input_)


ask = UserInput()
print(user_input)

If You wanted to use the value from the dictionary in say a function You could acces the value like this:

dict_name['key_name']

would access the value which can be a list, and integer, tuple, another dict, string and basically the variable name for that would be what is seen above so in a function it could be used like so:

your_func(user_input['user_input'][0])

for example if Your first argument to the function was the first item appended to the user_input['user_input'] list which in this case would be what the user inputed

Matiiss
  • 5,970
  • 2
  • 12
  • 29
  • Can you elaborate how this is ever going to work? You cannot use `input` in the application loop. `input` waits for an input. While the system is waiting for input, the application loop will halt and the game will not respond. – Rabbid76 Apr 16 '21 at 12:36
  • @Rabbid76 that I understand (now) but it was more of an example to also support my comment of using a dictionary in case the OP asked how to add to the dictionary from a class, obviously in his case there will be the enter press that will retun the inputed value and so on but this was just to show an example of how to add to a dictionary also will add how to use that value – Matiiss Apr 16 '21 at 12:40
  • I don't think that a novice programmer can apply this suggestions to his code. – Rabbid76 Apr 16 '21 at 12:44
  • @Rabbid76 well at least I tried and am always ready to answer questions if something is unclear, also for other people if they view the question a concept will be easier to apply than a direct answer since there is only a small chance someone will have the same exact issue but obviously a concept can be put in a direct answer too – Matiiss Apr 16 '21 at 12:46
  • You don't get my point. I fear that novice programmer copy your code and call `input` in the application loop. In my experience they just copy the code without reading the answer. – Rabbid76 Apr 16 '21 at 12:48
  • @Rabbid76 oh that I didn't think about, good point – Matiiss Apr 16 '21 at 12:50
0

Add a list attribute to the class InputBox and append the new input to the list:

class InputBox:

    def __init__(self, x, y, w, h, text=''):
        # [...]

        self.input_list = []

    def handle_event(self, event):
        if event.type == pg.MOUSEBUTTONDOWN:
            # If the user clicked on the input_box rect.
            if self.rect.collidepoint(event.pos):
                self.active = not self.active
            else:
                self.active = False
            self.color = COLOR_ACTIVE if self.active else COLOR_INACTIVE
        if event.type == pg.KEYDOWN:
            if self.active:
                if event.key == pg.K_RETURN:
                   
                    input_list.appned(self.text)

                    print(self.text)
                    self.text = ''

                elif event.key == pg.K_BACKSPACE:
                    self.text = self.text[:-1]
                else:
                    self.text += event.unicode
                self.txt_surface = FONT.render(self.text, True, self.color)

You can access the list of inputs of each box:

last_inout = input_boxes[0].input_list[-1]
Rabbid76
  • 202,892
  • 27
  • 131
  • 174