0

Hello Just like the title describes I am having issues with accessing a bool that is created in a class named "InputBox" and in this class there a function named "handle_event" and in the text monitoring function if the user inputs a certain string it will set a bool named "Login" to true. then in the main loop will then access that bool in the main loop and do actions with it (for now I just want to print it)

sorry if this is a dumb question I am a relative beginner and can't get anything to work

class InputBox:

    def __init__(self, x, y, w, h, text=''):
        self.rect = pygame.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 == pygame.MOUSEBUTTONDOWN:
            # If the user clicked on the input_box rect.
            if self.rect.collidepoint(event.pos):
                # Toggle the active variable.
                self.active = not self.active
            else:
                self.active = False
            # Change the current color of the input box.
            self.color = COLOR_ACTIVE if self.active else COLOR_INACTIVE
        if event.type == pygame.KEYDOWN:
            if self.active:
                if event.key == pygame.K_RETURN:
                    Userinput = str(self.text)
                    if Userinput == "tylerodell" or "wxenwxen":
                       #this is where I set the bool named Login to true (first cerated and then set to true)
                    self.text = ''
                elif event.key == pygame.K_BACKSPACE:
                    self.text = self.text[:-1]
                else:
                    self.text += event.unicode
                # Re-render the text.
                self.txt_surface = FONT.render(self.text, True, self.color)

    def update(self):
        # Resize the box if the text is too long.
        width = max(200, self.txt_surface.get_width()+10)
        self.rect.w = width

    def draw(self, screen):
        # Blit the text.
        screen.blit(self.txt_surface, (self.rect.x+5, self.rect.y+5))
        # Blit the rect.
        pygame.draw.rect(screen, self.color, self.rect, 2)


#This is all the Text that is used In the Program
TitleScreen_text1 = myfont1.render("Astronomers Notebook", False, (WHITE))
TitleScreen_text2 = myfont2.render("Version 0.1 Alpha", False, (WHITE))
TitleScreen_text3 = myfont2.render("Login To My Notebook", False, (WHITE))
TitleScreen_text4 = myfont2.render("UserName", False, (WHITE))
TitleScreen_text5 = myfont2.render("PassWord", False, (WHITE))

#this is all the varables that the program uses:
#this is for exiting the program
Run = True
#this is for running ceratn parts of the program
TitleScreen = True

#This is the main program:
while Run == True:
    while TitleScreen == True:
        clock = pygame.time.Clock()
        input_box1 = InputBox(500, 400, 140, 32)
        input_box2 = InputBox(500, 500, 140, 32)
        input_boxes = [input_box1, input_box2]
        done = False

        while not done:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    done = True
                    Run = False
                    pygame.quit()
                    sys.exit()
                for box in input_boxes:
                    box.handle_event(event)

            for box in input_boxes:
                box.update()

            screen.blit(Night_Sky, (0, 0))
            for box in input_boxes:
                box.draw(screen)

            #this is where I need to accsess the bool "Login"

            #this displays the text
            screen.blit(TitleScreen_text1,(250,200))
            screen.blit(TitleScreen_text2,(15,760))
            screen.blit(TitleScreen_text3,(450,300))
            screen.blit(TitleScreen_text4,(530,365))
            screen.blit(TitleScreen_text5,(530,465))
            pygame.display.flip()
            clock.tick(30)

Thank you in advance!

also I have tried setting it as a global variable and tried the first answer from here

1 Answers1

0

If the login status is independently configurable for each InputBox, you may add another instance attribute to InputBox corresponding to login, then access this attribute on your InputBox instance.

In __init__, you need to add:

self.login = False

Then, in your handle_event method:

if Userinput == "tylerodell" or "wxenwxen":
    #this is where I set the bool named Login to true (first cerated and then set to true)
    self.login = True

Now, you may access this in your while loop using:

#this is where I need to accsess the bool "Login"
for box in input_boxes:
    # Do something with login
    print(box.login) # Prints True or False       

Alternatively, if login status is shared across the InputBox instances, i.e. a single global as you mention, you could do something like:

login = False

class InputBox:
    ...

    if Userinput == "tylerodell" or "wxenwxen":
        #this is where I set the bool named Login to true (first cerated and then set to true)
        global login
        login = True

Then, later in your while loop, you can simply print(login) to find out the status.

dspencer
  • 4,297
  • 4
  • 22
  • 43
  • Thank you very much It worked. to anyone who stumbles across this later. there is a bug that makes only the bottom text box change the variable. If I fix it then I will update the code but other then that it worked amazing thank you very much! – Tyler O'dell Apr 07 '20 at 07:18