Module albow.widgets.Button
Source code
import logging
from pygame import Surface
from pygame import Rect
from pygame import draw
from albow.themes.Theme import Theme
from albow.widgets.ButtonBase import ButtonBase
from albow.widgets.Label import Label
class Button(ButtonBase, Label):
"""
A Button is a widget with a textual label which can be clicked with the mouse to trigger an action.
"""
def __init__(self, text, action=None, enable=None, **kwds):
"""
Args:
text: Initializes the button with the given text.
action: The action should be a function with no arguments; it is called
when the mouse is clicked and released again inside the button.
enable: If supplied, enable should be a function
that returns a boolean; it will be used to determine whether the button is enabled.
**kwds:
"""
self.logger = logging.getLogger(__name__)
if action:
kwds['action'] = action
if enable:
kwds['enable'] = enable
Label.__init__(self, text, **kwds)
self.border_color = Theme.BLACK
# self.border_width = 1
def draw_with(self, surface: Surface, fg: tuple, bg: tuple = None):
self.border_width = 0
super().draw_with(surface, fg, bg)
w = self._rect.width # syntactic sugar
h = self._rect.height # syntactic sugar
# draw border for normal button
draw.rect(surface, Theme.BLACK, Rect((0, 0, w, h)), 1) # black border around everything
draw.line(surface, Theme.WHITE, (1, 1), (w - 2, 1))
draw.line(surface, Theme.WHITE, (1, 1), (1, h - 2))
draw.line(surface, Theme.DARK_GRAY, (1, h - 1), (w - 1, h - 1))
draw.line(surface, Theme.DARK_GRAY, (w - 1, 1), (w - 1, h - 1))
draw.line(surface, Theme.GRAY, (2, h - 2), (w - 2, h - 2))
draw.line(surface, Theme.GRAY, (w - 2, 2), (w - 2, h - 2))
def __repr__(self):
return self.__class__.__name__
Classes
class Button (text, action=None, enable=None, **kwds)
-
A Button is a widget with a textual label which can be clicked with the mouse to trigger an action.
Args
text
- Initializes the button with the given text.
action
- The action should be a function with no arguments; it is called when the mouse is clicked and released again inside the button.
enable
- If supplied, enable should be a function that returns a boolean; it will be used to determine whether the button is enabled.
**kwds:
Source code
class Button(ButtonBase, Label): """ A Button is a widget with a textual label which can be clicked with the mouse to trigger an action. """ def __init__(self, text, action=None, enable=None, **kwds): """ Args: text: Initializes the button with the given text. action: The action should be a function with no arguments; it is called when the mouse is clicked and released again inside the button. enable: If supplied, enable should be a function that returns a boolean; it will be used to determine whether the button is enabled. **kwds: """ self.logger = logging.getLogger(__name__) if action: kwds['action'] = action if enable: kwds['enable'] = enable Label.__init__(self, text, **kwds) self.border_color = Theme.BLACK # self.border_width = 1 def draw_with(self, surface: Surface, fg: tuple, bg: tuple = None): self.border_width = 0 super().draw_with(surface, fg, bg) w = self._rect.width # syntactic sugar h = self._rect.height # syntactic sugar # draw border for normal button draw.rect(surface, Theme.BLACK, Rect((0, 0, w, h)), 1) # black border around everything draw.line(surface, Theme.WHITE, (1, 1), (w - 2, 1)) draw.line(surface, Theme.WHITE, (1, 1), (1, h - 2)) draw.line(surface, Theme.DARK_GRAY, (1, h - 1), (w - 1, h - 1)) draw.line(surface, Theme.DARK_GRAY, (w - 1, 1), (w - 1, h - 1)) draw.line(surface, Theme.GRAY, (2, h - 2), (w - 2, h - 2)) draw.line(surface, Theme.GRAY, (w - 2, 2), (w - 2, h - 2)) def __repr__(self): return self.__class__.__name__
Ancestors
Subclasses
- albow.demo.openGL.DemoButton.DemoButton
Inherited members
ButtonBase
:Label
:Label
:add
add_anchor
add_centered
align
anchor
attention_lost
augment_mouse_event
bg_color
bg_image
border_color
border_width
call_handler
call_parent_handler
computeSize
defer_drawing
disabled_bg_color
disabled_color
dismiss
draw
draw_over
enabled_bg_color
fg_color
focus
focus_switch
font
get_cursor
get_focus
get_margin_rect
get_root
get_top_widget
get_visible
global_to_local
has_focus
highlight_bg_color
highlight_color
inherited
invalidate
is_gl_container
key_down
key_up
local_to_global
margin
menu_bar
parent
parent_resized
present
rect
relative_mode
remove
remove_anchor
resized
scale_bg
sel_color
set_parent
set_size_for_text
tab_stop
text
visible