Skip to content

pct

pdwidgets.pct

pdwidgets.pct

Classes that dynamically calculate a percentage of a Widget's width or height.

Width/Height are lightweight value objects: pass one as a widget's w or h and it tracks the reference widget's size, so a child stays at (say) 50% of its parent even after the parent is resized.

Performance

The value is read straight from widget.width / widget.height (plain int(self._w) / int(self._h) accessors) rather than constructing a full widget.area Area object on every access, and the computed result is cached and only recomputed when the reference dimension actually changes. This keeps the common case (repeated reads while the widget is not resizing) down to a single attribute read and comparison, which matters on MicroPython where the old design allocated an Area and re-multiplied on every arithmetic op and comparison.

Height(percent, widget)

Bases: _Percent

A value object that tracks a percentage of a Widget's height.

Parameters:

Name Type Description Default
percent int | float

The percentage of the height of the widget (0-100).

required
widget Widget

The widget whose height drives the calculation.

required

Raises:

Type Description
ValueError

If the percent is not between 0 and 100.

AttributeError

If the widget has no attribute 'area'.

Returns:

Type Description
int

The calculated percentage of the height of the widget.

Usage

import pdwidgets as pd ... widget = pd.Widget(parent, h=100) my_height = pd.pct.Height(50, widget) print(my_height) # 50 widget.height = 200 print(my_height) # 100

Width(percent, widget)

Bases: _Percent

A value object that tracks a percentage of a Widget's width.

Parameters:

Name Type Description Default
percent int | float

The percentage of the width of the widget (0-100).

required
widget Widget

The widget whose width drives the calculation.

required

Raises:

Type Description
ValueError

If the percent is not between 0 and 100.

AttributeError

If the widget has no attribute 'area'.

Returns:

Type Description
int

The calculated percentage of the width of the widget.

Usage

import pdwidgets as pd ... widget = pd.Widget(parent, w=100) my_width = pd.pct.Width(50, widget) print(my_width) # 50 widget.width = 200 print(my_width) # 100