Easily create interactible button objects in PyGame with just a few short lines of code.
Project description
EasyButton
Easily create interactable button objects in PyGame with just a few short lines of code.
EasyButton is used to create button objects from class objects defined in the easy_button.py
module file. These buttons are fully customizable; can be updated and moved in-execution of code; and contain several methods for displaying hover effects and executing commands when clicked. As of this first launch version, the only button object is the 'rect' rectangle button. This module is not a professional product; rather, it is made by a student for other students as an alternative to the tedious, excessive amount of code it takes to create a working, interactive button in PyGame.
If you would like to Say Thanks for this project, then please .
Button Class Objects
To create a button object, import easy_button
, and set my_button = easy_button.button_object()
. No object requires values to be passed to it to initiate, though such will be required to make the button usable. Following are headings for each button object; then comes a bulleted list of the object's methods — the functions that can be called upon the button to make it operate — and what they do. Each method is followed by a sublist of the values that can be passed to it and the same's defaults. The final bullet point is another sublist, of certain attributes of the whole class that are not covered by the method documentation, but may be of interest to the programmer; not all other attributes are present, however.
EasyButton Attributes
Variables of the EasyButton module that do not belong to any of its subclasses.
__version__
A variable that contains the version of EasyButton installed in your directory; the version that is being used.
rect()
Rectangular button object.
-
__init__(pos=(0, 0), width=100, height=50, text='', bg=(255, 255, 255), fg=(0, 0, 0), font='timesnewroman', pixel=False, thickness=0, command=None, border=0, margin=10, bc=(255, 0, 0), hover_bg=None, hover_fg=None, hover_border=5, hover_bc=(0, 255, 0), hover_text=None, hover_margin=None, hover_font=None, hover_pixel=False, image=None, hover_image=None)
This method is run upon creating the button object; it stores the buttons properties.pos = (0, 0)
Position of top-left corner of button objectwidth = 100
Widthheight = 50
Heightborder = 0
Thickness of border with which to wrap buttontext = ""
Text to display on button; defaults to no textfont = "timesnewroman"
Font to use on textpixel = False
Pixelate text or notmargin = 10
Margin of spacing to put betweentext
andimage
and border/button edge; negative value blows text out of buttonimage = None
Image to display on button background, behindtext
; will be automatically scaled smaller — not larger — to fit usingmargin
, liketext
; takes either image file path or predefined PyGame Image object — note that the former sometimes fails (known error), and is not advised; defaults to no imagebg = (255, 255, 255)
Background color of button; defaults to blackbc = (255, 0, 0)
Border color; defaults to redfg = (0, 0, 0)
Foreground (text) color; defaults to whitethickness = 0
Thickness of button background; does not inhibit placement of objects of the button; value less than width/height results in transparent button center; defaults to solid backgroundcommand = None
Function to execute when button is clickedhover_border = 5
Thickness of border to wrap button with while the same is touching the mouse cursorhover_text = None
Text to display when button is touching mouse cursor; defaults totext
hover_font = None
Font to use on text displayed while button is touching the mouse cursor; defaults totext
hover_pixel = False
Pixelate the text displayed when button is touching the mouse cursor or nothover_margin = None
Margin of spacing to put between text and border/button edge while the same is touching the mouse cursor; negative value blows text out of button; defaults tomargin
hover_image = None
Image to display on button background, behind text, when button is touching the mouse cursor; will automatically be scaled to size like text usinghover_margin
; takes either image file path or predefined PyGame Image object — note that the former sometimes fails, and is not advised; defaults toimage
hover_bg = None
Background color of button while the same is touching the mouse cursor; defaults tobg
hover_bc = (0, 255, 0)
Border color while button is touching mouse cursor; defaults to greenhover_fg = None
Foreground (text) color while button is touching the mouse cursor; defaults tofg
-
update(self)
This method updates the button object'ssurface
attribute so as to make it display updated properties. It is automatically run by thecheck_hover
method when a change inhovering
status is detected, or the text it is displaying varies from the text it should be displaying. -
draw(self, destination
This method blits the button onto a PyGame Surface.destination
The PyGame Surface that the button is to be drawn to
-
hover(self, point)
This method checks to see if the button is touching the mouse cursor; if it is, it updates the button's Surface to display the values specially entered for when the button is being hovered over.point
A tuple or list containing thex
andy
positions of the mouse
-
check_hover(self, point)
This method checks to see whether the button is touching the mouse cursor and returns the according boolean value.point
A tuple or list containing thex
andy
positions of the mouse
-
click(self, point, down)
This method checks whether or not the button has been clicked. If it has, then it executes thecommand
function attribute of the button.point
A tuple or list containing thex
andy
positions of the mousedown
A boolean value stating whether or not the mouse is down
-
check_click(self, point, down)
This method checks whether or not the button has been clicked, and returns the according boolean value.point
A tuple or list containing thex
andy
positions of the mousedown
A boolean value stating whether or not the mouse is down
-
get_fonts(self)
This method calls the PyGamefont.get_fonts
method and returns a list of all the available fonts that can be given as values forfont
andhover_font
. -
Attributes
surface
The PyGame surface object that contains the image of the button which is updated by theupdate
method and blitted to a PyGame Surface object by thedraw
method.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for easy_button-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 46d5b807396aa0b4554f7cf9733b68617499c330a6b8347cdaf6f7ece4051f85 |
|
MD5 | 189a00902806d2d8c809436e30e9696b |
|
BLAKE2b-256 | 1dea3f0c15a39c1841dd40e1ff9d0caf4478657478c43d136696dd51cd6755c2 |