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 .
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.
Attributes 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.exampleAn example project that shows some button objects, how to use them, what they are capable of, and other extra actions that can be performed.
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 = 100Widthheight = 50Heightborder = 0Thickness of border with which to wrap buttontext = ""Text to display on button; defaults to no textfont = "timesnewroman"Font to use on textpixel = FalsePixelate text or notmargin = 10Margin of spacing to put betweentextandimageand border/button edge; negative value blows text out of buttonimage = NoneImage 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 = 0Thickness 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 = NoneFunction to execute when button is clickedhover_border = 5Thickness of border to wrap button with while the same is touching the mouse cursorhover_text = NoneText to display when button is touching mouse cursor; defaults totexthover_font = NoneFont to use on text displayed while button is touching the mouse cursor; defaults totexthover_pixel = FalsePixelate the text displayed when button is touching the mouse cursor or nothover_margin = NoneMargin 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 tomarginhover_image = NoneImage 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 toimagehover_bg = NoneBackground color of button while the same is touching the mouse cursor; defaults tobghover_bc = (0, 255, 0)Border color while button is touching mouse cursor; defaults to greenhover_fg = NoneForeground (text) color while button is touching the mouse cursor; defaults tofg
-
update(self)
This method updates the button object'ssurfaceattribute so as to make it display updated properties. It is automatically run by thecheck_hovermethod when a change inhoveringstatus 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.destinationThe 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.pointA tuple or list containing thexandypositions 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.pointA tuple or list containing thexandypositions of the mouse
-
click(self, point, down)
This method checks whether or not the button has been clicked. If it has, then it executes thecommandfunction attribute of the button.pointA tuple or list containing thexandypositions of the mousedownA 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.pointA tuple or list containing thexandypositions of the mousedownA boolean value stating whether or not the mouse is down
-
get_fonts(self)
This method calls the PyGamefont.get_fontsmethod and returns a list of all the available fonts that can be given as values forfontandhover_font. -
Attributes
surfaceThe PyGame surface object that contains the image of the button which is updated by theupdatemethod and blitted to a PyGame Surface object by thedrawmethod.