Skip to main content
Engineering LibreTexts

6.20: Converting from Pixel Coordinates to Buttons

  • Page ID
    14550
  • def getButtonClicked(x, y):
        if YELLOWRECT.collidepoint( (x, y) ):
            return YELLOW
        elif BLUERECT.collidepoint( (x, y) ):
            return BLUE
        elif REDRECT.collidepoint( (x, y) ):
            return RED
        elif GREENRECT.collidepoint( (x, y) ):
            return GREEN
        return None
    
    
    if __name__ == '__main__':
        main()
    

    The getButtonClicked() function simply takes XY pixel coordinates and returns either the values YELLOW, BLUE, RED, or GREEN if one of the buttons was clicked, or returns None if the XY pixel coordinates are not over any of the four buttons.

    • Was this article helpful?