Skip to content

Py-GameUI

A Simple module for making GUIs in pygame. You can make Buttons, Input Boxes, Sliders and gradient rectangle with this module.

Button

Button element

__init__(self, relative_rect, enabled=True, text='', image=None, image_x=0, image_y=0, bg=(124, 124, 124), fg=(255, 255, 255), cc=(13, 80, 213), hc=(160, 160, 160), border=2, border_color=(0, 0, 0), border_radius=(0, 0, 0, 0), font=None, ipadx=0, ipady=0, function=None) special

Initilaizes the Button element.

Parameters:

Name Type Description Default
relative_rect Union[pygame.Rect, tuple, list]

The coordinates and the size of the button

required
enabled bool

If you want the button to be whether enabled or disabled. Default to True

True
text str

The text you want to display in the button

''
image Surface

The image you want to blit

None
image_x int

X Coordinate of the image

0
image_y int

Y Coordinate of the image

0
bg Union[tuple, pygame.Color]

The background color of the button

(124, 124, 124)
fg Union[tuple, pygame.Color]

Color of the font

(255, 255, 255)
cc Union[tuple, pygame.Color]

Color of the button when clicked

(13, 80, 213)
hc Union[tuple, pygame.Color]

Color of the button when hovered

(160, 160, 160)
border int

The width of the border you want

2
border_color Union[tuple, pygame.Color]

Color of the border of the Button

(0, 0, 0)
border_radius Union[int, tuple, list]

Border radius for the button

(0, 0, 0, 0)
font Font

Font of the text you want to display

None
ipadx int

The internal x-padding of the button

0
ipady int

The internal y-padding of the button

0
function

The function you want to call when the button is pressed

None

draw(self, win)

Draws the button to the screen

Parameters:

Name Type Description Default
win Surface

The surface to draw the button to

required

events(self, event)

Function to handle all the events with the button like clicking, hovering etc.

Parameters:

Name Type Description Default
event

Events from pygame window.

required

Input_box

Input box for receiving one line input

__init__(self, relative_rect, bg_color=(124, 124, 124), active_color=(255, 255, 255), font=None, fg=(0, 0, 0), border=0, border_color=(0, 0, 0)) special

Initilaizes the input box.

Parameters:

Name Type Description Default
relative_rect Union[pygame.Rect, tuple, list]

Coordinates and size of the input box

required
bg_color

Background color of the input box

(124, 124, 124)
active_color

Background color of the input box when it is focused

(255, 255, 255)
font

Font for the text

None
fg

Font color for the input box

(0, 0, 0)
border

Size of the border

0
border_color

Color of the border

(0, 0, 0)

draw(self, window)

Draws the input box to the screen :param win: The surface to draw the button to :type win: class:pygame.Surface

events(self, event)

A function to handle all the events :param event: The event object to look for :type event: class:pygame.event.Event

Slider

__init__(self, x, y, from_=0, to_=3, part_size=10, bg=(54, 54, 54), cc=(13, 80, 213), hc=(160, 160, 160), fg=(255, 255, 255), thumb_bg=(124, 124, 124), font=None, show_numbers=True) special

A Slider GUI for pygame.

Parameters:

Name Type Description Default
x int

The X position of the slider

required
y int

The Y position of the slider

required
from_ int

Where The value starts from

0
to_ int

Where the value ends

3
part_size int

The size for one part in the slider

10
bg tuple

Background color of the Slider

(54, 54, 54)
cc tuple

Color when the thumb is clicked

(13, 80, 213)
hc tuple

Color when the thumb is hovered

(160, 160, 160)
fg tuple

Color of the font to be displayed

(255, 255, 255)
thumb_bg tuple

Background color of the thumb

(124, 124, 124)
font Font

The font for the number displayed

None
show_numbers bool

True if you want the numbers to be shown

True

draw(self, window)

A function that draws the slider into the pygame screen

Parameters:

Name Type Description Default
window Surface

The window to draw the slider to

required

events(self, event)

A function to handle all events like clicking, moving, hovering and more :param event: The event object to look for :type event: class:pygame.event.Event

gradient_rect(window, left_colour, right_colour, relative_rect)

Draw a horizontal-gradient filled rectangle covering

Parameters:

Name Type Description Default
window Surface

Screen to blit the gradient rect

required
left_colour Union[tuple, list, pygame.Color]

Color in the left corner

required
right_colour Union[tuple, list, pygame.Color]

Color in the right corner

required
relative_rect Rect

Position and width to draw the gradient rect

required
Back to top