Core classes/modules:

Advanced:

Experimental/Broken:

Creating a Button

A button can be created simply by making a rectangle which detects the mouse position over the rectangle and a button press at the same time.

import pygame as pg

class Button:
    def __init__(self, rect, command):
        self.rect = pg.Rect(rect)
        self.image = pg.Surface(self.rect.size).convert()
        self.image.fill((255,0,0))
        self.function = command
        
    def get_event(self, event):
        if event.type == pg.MOUSEBUTTONDOWN and event.button == 1:
            self.on_click(event)
            
    def on_click(self, event):
        if self.rect.collidepoint(event.pos):
            self.function()
            
    def draw(self, surf):
        surf.blit(self.image, self.rect)
        
def on_press():
    print('pressed button')
        
screen = pg.display.set_mode((800,600))
done = False
btn = Button(rect=(50,50,105,25), command=on_press)

while not done:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True
        btn.get_event(event)
    btn.draw(screen)
    pg.display.update()

python-gaming.com