patternpythonMinor
GUI Elements for pygame games
Viewed 0 times
elementsgamesforpygamegui
Problem
I've been making games using the Pygame module recently. I've noticed that Pygame has no builtin GUI elements. To save time for me (and hopefully other people), I have created some very simple GUI elements: buttons and labels.
The button module was the first to be created, and is probably the most thoroughly tested. The button module has a button class. The class has main methods to create and update the button. the
I've included doc strings on all methods, so I should not have to explain the code too much:
```
import pygame as pg
pg.init()
class Button:
def __init__(self, surface, x, y, width, height):
"""
Creates a Button that can be used in any pygame project.
Arguments:
surface -- a pygame.display.set_mode() object.
x -- the x axis position of the button's top left corner
y -- the y axis position of the button's top left corner
width -- the width of the button
height -- the height og the button
the syntax of creating a instance of the class:
btn_obj = Button(surface, x, y, width, height)
To display the button and update the button, acording the the mouse
position, two methods must be called:
btn_obj.render_button() -- this method is used to render the button.
Call the function after Updating the pygame screen in your
Pygame window event loop.
btn_obj.update_button() -- this method is used to change the buttons
sytle and to run a specifed function, depending on the mouse position.
Call this function in your Pygame window event loop.
"""
# getting surface/window/display reference
self.surface = surface
# setting button dimensions
self.x = x
self.y = y
self.width = width
self.height = height
# button rectangle and
The button module was the first to be created, and is probably the most thoroughly tested. The button module has a button class. The class has main methods to create and update the button. the
__init__ method, render_button method, and the update_button method. I've included doc strings on all methods, so I should not have to explain the code too much:
```
import pygame as pg
pg.init()
class Button:
def __init__(self, surface, x, y, width, height):
"""
Creates a Button that can be used in any pygame project.
Arguments:
surface -- a pygame.display.set_mode() object.
x -- the x axis position of the button's top left corner
y -- the y axis position of the button's top left corner
width -- the width of the button
height -- the height og the button
the syntax of creating a instance of the class:
btn_obj = Button(surface, x, y, width, height)
To display the button and update the button, acording the the mouse
position, two methods must be called:
btn_obj.render_button() -- this method is used to render the button.
Call the function after Updating the pygame screen in your
Pygame window event loop.
btn_obj.update_button() -- this method is used to change the buttons
sytle and to run a specifed function, depending on the mouse position.
Call this function in your Pygame window event loop.
"""
# getting surface/window/display reference
self.surface = surface
# setting button dimensions
self.x = x
self.y = y
self.width = width
self.height = height
# button rectangle and
Solution
You realize that your
then the second call will reset the caption to
Also, there are other pygame GUI libraries so you might check there before reinventing the wheel.
.decorate method will, when called, reset any parameter that isn't set? So if I call:button.decorate(caption='mycaption')
button.decorate(font_size=32)then the second call will reset the caption to
"" ?Also, there are other pygame GUI libraries so you might check there before reinventing the wheel.
Code Snippets
button.decorate(caption='mycaption')
button.decorate(font_size=32)Context
StackExchange Code Review Q#138810, answer score: 2
Revisions (0)
No revisions yet.