1
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()

If it helps at all to know, I need to only store integer, values, thanks! The main section of the code that the problem lies is near the bottom under the function "zScore", but it does use some of the classes created throughout the code. Any help is appreciated.

What I tried to do at first is store the variable text in another variable and do basic operations using this input variable, but of course the input box just accepts strings. I then tried to use the int() function to try to convert the string entered into the input box into an integer but this did not work either. I haven't been able to use multiple input boxes on the same screen either.

  • 1
    I know that you're using my example, but that's a lot of code in your question which we have to analyze in order to give an answer. It would be helpful if you reduced it to a [minimal, runnable example](https://stackoverflow.com/help/mcve). – skrx Oct 31 '18 at 18:34
  • 1
    A link to my original example: https://stackoverflow.com/a/46390412/6220679 – skrx Oct 31 '18 at 18:59

1 Answers1

2

You just need to convert the text variable into an int when the user presses enter. This should work:

if event.key == pygame.K_RETURN:
    try:
        input1 = int(text)
    except ValueError:
        print('Invalid input')
    text = ""
skrx
  • 19,980
  • 5
  • 34
  • 48
  • I'm not sure what you want to do with the `input1` afterwards. If you want to use it in one of the other functions, you'd have to `return` it. – skrx Oct 31 '18 at 19:07
  • Hi, sorry, I just saw your comment. Yes, this is your algorithm I'm sorry if I should have asked first, your posts are very helpful to me. Thank you for responding to my pygame question yet again, you have been very helpful! – John Gunasar Nov 08 '18 at 02:02