从python中的类访问布尔

问题描述 投票:0回答:1

你好,就像标题中描述的那样,我在访问在名为“ InputBox”的类中创建的布尔时遇到问题,在该类中,如果用户输入了某个字符串,则在该类中有一个名为“ handle_event”的函数以及在文本监视函数中它将名为“ Login”的布尔值设置为true。然后在主循环中将在主循环中访问该布尔值并对其进行操作(现在我只想打印它)

对不起,如果这是一个愚蠢的问题,我是一个相对初学者,什么都无法工作

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)

谢谢你!

我也尝试将其设置为全局变量,并尝试了来自here的第一个答案

python class
1个回答
0
投票

您可以在InputBox实例上向login添加另一个实例属性,然后在InputBox实例上访问此属性。

__init__中,您需要添加:

self.login = False

然后,在您的handle_event方法中:

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

现在,您可以在while循环中使用:

#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       
© www.soinside.com 2019 - 2024. All rights reserved.