Python 如何在pygame的一个表面上有多个输入框,并将这些输入存储在变量中?

Python 如何在pygame的一个表面上有多个输入框,并将这些输入存储在变量中?,python,pygame,Python,Pygame,如果它有助于了解,我只需要存储整数,值,谢谢! 问题所在的代码主要部分位于函数“zScore”的底部附近,但它确实使用了在整个代码中创建的一些类。感谢您的帮助 我首先尝试将变量文本存储在另一个变量中,并使用此输入变量执行基本操作,但输入框当然只接受字符串。然后,我尝试使用int()函数将输入框中的字符串转换为整数,但这也不起作用。我也无法在同一屏幕上使用多个输入框。当用户按enter键时,只需将文本变量转换为int。这应该起作用: import pygame pygame.init() dis

如果它有助于了解,我只需要存储整数,值,谢谢! 问题所在的代码主要部分位于函数“zScore”的底部附近,但它确实使用了在整个代码中创建的一些类。感谢您的帮助


我首先尝试将变量文本存储在另一个变量中,并使用此输入变量执行基本操作,但输入框当然只接受字符串。然后,我尝试使用int()函数将输入框中的字符串转换为整数,但这也不起作用。我也无法在同一屏幕上使用多个输入框。

当用户按enter键时,只需将
文本
变量转换为int。这应该起作用:

import pygame
pygame.init()

display_width = 1024
display_height = 600
display = pygame.display.set_mode((display_width,display_height))

blue = (0,0,200)
light_blue = (0,0,255)
black = (0,0,0)
red = (200,0,0,255)
light_red = (255,0,0)
white = (255,255,255)
light_purple = (139,0,139,255)
purple = (128,0,128,255)

smallfont = pygame.font.SysFont("comicsansms", 50)
medfont = pygame.font.SysFont("Algerian", 50)
largefont = pygame.font.SysFont("Algerian", 85)


altSmall = pygame.font.SysFont("Algerian",20)
altMed = pygame.font.SysFont("Algerian",30)
altLarge = pygame.font.SysFont("Algerian",75)
extrasmall = pygame.font.SysFont("comicsansms",15)


clock = pygame.time.Clock()

def text_objects(text, color,size = "small"):

    textSurface = smallfont.render(text, True, black)

    if size == "small":
        textSurface = smallfont.render(text, True, color)
    if size == "medium":
        textSurface = medfont.render(text, True, color)
    if size == "large":
        textSurface = largefont.render(text, True, color)

    if size == "altMed":
        textSurface = altMed.render(text,True,color)

    if size == "altSmall":
        textSurface = altSmall.render(text,True,color)

    if size == "medFont":
        textSurface = medfont.render(text,True,color)

    if size == "smallFont":
        textSurface = smallfont.render(text,True,color)

    if size == "extraSmall":
        textSurface = extrasmall.render(text,True,color)

    return textSurface, textSurface.get_rect()

def alt_med_pos_message_to_screen(msg,color,x,y,size = "smallfont"):
    screen_text = smallfont.render(msg,True,color)
    display.blit(screen_text,[x,y])

class Button():

    text = str()
    x = int()
    y = int()
    width = int()
    height = int()
    inactive_color = None
    active_color = None
    action = str()
    active = bool()

    def __init__(self,text,x,y,width,height,inactive_color,active_color,action,text_color,text_size,active):
        self.text = text
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.inactive_color = inactive_color
        self.active_color = active_color
        self.action = action
        self.text_color = text_color
        self.text_size = text_size
        self.active = active

    def nothing():
        pass

    def handle_event(self,event):

        if self.is_hovered() and event.type == pygame.MOUSEBUTTONDOWN:
            self.action()

    def text_to_button(self,surface):
        textSurf, textRect = text_objects(self.text,self.text_color,self.text_size)
        textRect.center = ((self.x+(self.width/2)), self.y+(self.height/2))
        surface.blit(textSurf, textRect)


    def is_hovered(self):
        cur = pygame.mouse.get_pos()
        return self.x + self.width > cur[0] > self.x and self.y + self.height > cur[1] > self.y

    def drawButton(self, surface):

        if self.active:
            pygame.draw.rect(surface,self.inactive_color,[self.x,self.y,self.width,self.height])

        if self.is_hovered() and self.active:
            pygame.draw.rect(surface, self.active_color,[self.x,self.y,self.width,self.height])

        self.text_to_button(surface)

def home_screen():

    backgroundImg = 'C:/Users/student/Desktop/Geometry Calc/Images/programbackground.png'
    statisticsImg = 'C:/Users/student/Desktop/Geometry Calc/Images/statistics.png'
    algebraImg = 'C:/Users/student/Desktop/Geometry Calc/Images/algebra-math.png'
    geometryImg = 'C:/Users/student/Desktop/Geometry Calc/Images/geometry.png'

    background = pygame.image.load(backgroundImg)
    statistics = pygame.image.load(statisticsImg)
    algebra = pygame.image.load(algebraImg)
    geometry = pygame.image.load(geometryImg)

    homeScreen = True
    StatisticsButton = Button("Statistics",100,250,250,85,purple,light_purple,statistics_formulas,black,"smallfont",True)
    GeometryButton = Button("Geometry",100,315,250,85,purple,light_purple,geometry_formulas,black,"smallfont",True)
    AlgebraButton = Button("Algebra",100,390,250,75,purple,light_purple,algebra_formulas,black,"smallfont",True)
    while homeScreen:
        for evt in pygame.event.get():
            StatisticsButton.handle_event(evt)
            GeometryButton.handle_event(evt)
            AlgebraButton.handle_event(evt)
            if evt.type == pygame.QUIT:
                homeScreen = False
                pygame.quit()
                quit()

        display.blit(background,[0,0])
        alt_med_pos_message_to_screen("Formula Hack",red,display_width/2-150,100,size = "smallfont")
        StatisticsButton.drawButton(display)
        GeometryButton.drawButton(display)
        AlgebraButton.drawButton(display)

        display.blit(statistics,[350,250])
        display.blit(geometry,[350,325])
        display.blit(algebra,[350,400])

        pygame.display.update()

def statistics_formulas():

    inStatistics = True
    backgroundImg ='C:/Users/student/Desktop/Geometry Calc/Images/StatisticsBackground.png'

    Zscore = Button("1. Z-score",900,25,75,35,purple,light_purple,zscore,black,"extraSmall",True)


    statsBackground = pygame.image.load(backgroundImg)
    while inStatistics:
        for evt in pygame.event.get():
            Zscore.handle_event(evt)
            if evt.type == pygame.QUIT:
                inStatistics = False
                pygame.quit()
                quit()

        display.blit(statsBackground,[0,0])

        Zscore.drawButton(display)
        pygame.display.update()

def geometry_formulas():
    print("geometry")

def algebra_formulas():
    print("algebra")

def zscore():
    screen = pygame.display.set_mode((display_width, display_height))
    font = pygame.font.Font(None, 32)
    clock = pygame.time.Clock()
    input_box = pygame.Rect(100, 100, 140, 32)
    color_inactive = pygame.Color('lightskyblue3')
    color_active = pygame.Color('dodgerblue2')
    color = color_inactive
    active = False
    text = "0"
    done = False
    input1 = int(text)

    while not done:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True
            if event.type == pygame.MOUSEBUTTONDOWN:
                # If the user clicked on the input_box rect.
                if input_box.collidepoint(event.pos):
                    # Toggle the active variable.
                    active = not active
                else:
                    active = False
                # Change the current color of the input box.
                color = color_active if active else color_inactive
            if event.type == pygame.KEYDOWN:
                if active:
                    if event.key == pygame.K_RETURN:
                        text = ""
                    elif event.key == pygame.K_BACKSPACE:
                        text = text[:-1]
                    else:
                        text += event.unicode

        screen.fill((30, 30, 30))
        # Render the current text.
        txt_surface = font.render(text, True, color)
        # Resize the box if the text is too long.
        width = max(200, txt_surface.get_width()+10)
        input_box.w = width
        # Blit the text.
        screen.blit(txt_surface, (input_box.x+5, input_box.y+5))

        # Blit the input_box rect.
        pygame.draw.rect(screen, color, input_box, 2)

        pygame.display.flip()
        clock.tick(30)

home_screen()

我知道你在用我的例子,但你的问题中有很多代码,我们必须分析这些代码才能给出答案。如果您将其简化为一个.a链接,指向我的原始示例:我不确定以后您希望如何处理
input1
。如果你想在其他函数中使用它,你必须返回它。嗨,对不起,我刚刚看到你的评论。是的,这是你的算法我很抱歉如果我应该先问一下,你的帖子对我很有帮助。再次感谢您回答我的pygame问题,您帮了我很大的忙!
if event.key == pygame.K_RETURN:
    try:
        input1 = int(text)
    except ValueError:
        print('Invalid input')
    text = ""