Module albow.widgets.ButtonBase

Source code
from pygame import event

from albow.widgets.Control import Control


class ButtonBase(Control):
    """
    ButtonBase is a mixin class for use by widgets having button-like behaviour. It provides handlers for
    mouse events that maintain a "highlighted" state, support for enabling and disabling the button, and s
    upport for calling a function when the button is clicked.

    It does not provide any functionality for drawing; that is the responsibility of the subclass using it.
    """
    align = 'c'
    action = None
    """
    A function of no arguments to be called when the button is clicked. May also be defined as 
    a method in the subclass.
    """

    def mouse_down(self, theEvent: event):
        if self.enabled:
            self._highlighted = True

    def mouse_drag(self, theEvent: event):

        state = theEvent in self

        if state != self._highlighted:
            self._highlighted = state
            self.invalidate()

    def mouse_up(self, theEvent: event):
        if theEvent in self:
            self._highlighted = False
            if self.enabled:
                self.call_handler('action')     # TODO Fix this by using another 'mixin' shared with the Widget class

    def get_highlighted(self):
        return self._highlighted

    def get_enabled(self):
        enable = self.enable
        if enable:
            return enable()
        else:
            return self._enabled

    def set_enabled(self, theNewValue: bool):
        self._enabled = theNewValue

Classes

class ButtonBase (*args, **kwargs)

ButtonBase is a mixin class for use by widgets having button-like behaviour. It provides handlers for mouse events that maintain a "highlighted" state, support for enabling and disabling the button, and s upport for calling a function when the button is clicked.

It does not provide any functionality for drawing; that is the responsibility of the subclass using it.

Source code
class ButtonBase(Control):
    """
    ButtonBase is a mixin class for use by widgets having button-like behaviour. It provides handlers for
    mouse events that maintain a "highlighted" state, support for enabling and disabling the button, and s
    upport for calling a function when the button is clicked.

    It does not provide any functionality for drawing; that is the responsibility of the subclass using it.
    """
    align = 'c'
    action = None
    """
    A function of no arguments to be called when the button is clicked. May also be defined as 
    a method in the subclass.
    """

    def mouse_down(self, theEvent: event):
        if self.enabled:
            self._highlighted = True

    def mouse_drag(self, theEvent: event):

        state = theEvent in self

        if state != self._highlighted:
            self._highlighted = state
            self.invalidate()

    def mouse_up(self, theEvent: event):
        if theEvent in self:
            self._highlighted = False
            if self.enabled:
                self.call_handler('action')     # TODO Fix this by using another 'mixin' shared with the Widget class

    def get_highlighted(self):
        return self._highlighted

    def get_enabled(self):
        enable = self.enable
        if enable:
            return enable()
        else:
            return self._enabled

    def set_enabled(self, theNewValue: bool):
        self._enabled = theNewValue

Ancestors

Subclasses

Class variables

var action

A function of no arguments to be called when the button is clicked. May also be defined as a method in the subclass.

var align

Methods

def get_enabled(self)
Source code
def get_enabled(self):
    enable = self.enable
    if enable:
        return enable()
    else:
        return self._enabled
def get_highlighted(self)
Source code
def get_highlighted(self):
    return self._highlighted
def mouse_down(self, theEvent)
Source code
def mouse_down(self, theEvent: event):
    if self.enabled:
        self._highlighted = True
def mouse_drag(self, theEvent)
Source code
def mouse_drag(self, theEvent: event):

    state = theEvent in self

    if state != self._highlighted:
        self._highlighted = state
        self.invalidate()
def mouse_up(self, theEvent)
Source code
def mouse_up(self, theEvent: event):
    if theEvent in self:
        self._highlighted = False
        if self.enabled:
            self.call_handler('action')     # TODO Fix this by using another 'mixin' shared with the Widget class
def set_enabled(self, theNewValue)
Source code
def set_enabled(self, theNewValue: bool):
    self._enabled = theNewValue

Inherited members