Skip to main content

A Pygame library of a button class with properly-centered text!

Project description

Pygame

TRULY CENTERED BUTTON CLASS

Sick and tired of Pygame's uncentered text on your buttons? Look no further!

my_button = Button(params)

Required parameters

  • screen: pygame.Surface
  • text: str
  • color: tuple (r, g, b)
  • center: tuple (x, y)
  • dim: tuple (w, h)

Optional parameters

  • thickness: int
  • radius: int
  • font_size: int

Additional Attributes

  • real_rect: Actual surrounding border seen on screen
  • font_rect: Invisible border that wraps the text
  • screen_color: Screen color behind the button
  • clicked: Returns whether the button has been clicked

Methods

  • draw(): Draws the button (recommended to run immediately after instantiation)
  • draw_text(): Draws only the text without the bordering rectangle
  • is_hovered(): Checks if button is hovered (does not update visual state)
  • is_clicked(): Updates visual state of button and returns whether the button was just clicked

Quickstart

shell

pip install pygame-truly-centered-button

python

#example.py

import pygame
from pgcenteredbutton import Button, BadButton 

if __name__ == "__main__":
    
    pygame.init()
    clock = pygame.time.Clock()

    screen_dim = (800, 200)
    screen = pygame.display.set_mode(screen_dim)
    button_color = (200, 200, 200)
    button_dim = (300, 100)
    
    good_button_center = (screen_dim[0]//4, screen_dim[1]//2)
    good_button = Button(
        screen = screen,
        text = 'GOOD', 
        color = button_color, 
        center = good_button_center, 
        dim = button_dim)
    good_button.draw()

    bad_button_center = (screen_dim[0]*3//4, screen_dim[1]//2)
    bad_button = BadButton(
        screen = screen, 
        text = 'BAD', 
        color = button_color, 
        center = bad_button_center, 
        dim = button_dim)
    bad_button.draw()

    while good_button.clicked is False:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                exit()

            if good_button.is_clicked(event):
                print('Goodbye world')

            if bad_button.is_clicked(event):
                print('I don\'t matter!')
                
        clock.tick(60)
        pygame.display.update()
    
    pygame.quit()
    exit()

WHAT A DIFFERENCE!

Button Demo

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pygame-truly-centered-button-2.8.tar.gz (4.6 kB view details)

Uploaded Source

File details

Details for the file pygame-truly-centered-button-2.8.tar.gz.

File metadata

File hashes

Hashes for pygame-truly-centered-button-2.8.tar.gz
Algorithm Hash digest
SHA256 ab68371cb71a3f7d8270022fd0b528c8d2adc75a15f61b8ec2bd4d8abeb39b0b
MD5 f40ac9b48aee6e328ea465ccf8add44e
BLAKE2b-256 96198c2850443cc7aaeaf6cf6142715d4788f3855bed62320a8ec18abf4a99c4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page