boxes.box

class boxes.box.Box(context, rect=None, aspect=None, **kwargs)

Represents a rectangle in the latout. The position of the rectangle is given by the rect property.

All keyword arguments to the constructor are optional, and the same effect can be obtained by later constraining the box.

Parameters:

In addition, any property of Rect can be used as a keyword argument to the constructor, which will introduce a constraint.

Note, that Box instances can also be obtained by calling the box() method of a Context instance.

Methods

solve(fix_upper_left=True)

Calling this method has two effects:

  1. If fix_upper_left is True (the default), a constraint is added setting self.loc == (0, 0).
  2. solve() is called on Box.context.
>>> from boxes import Context
>>> ctx = Context()
>>> box = ctx.box(size=(6.0, 10.0))
>>> (box.width, box.height)
(Expr(x1 - x3), Expr(-x0 + x2))
>>> box.solve()
>>> (box.width, box.height)
(6.0, 10.0)
fix(other)

Constrain this box and other to have the same size and position.

pad(*args)

Construct a new box similar to this, but smaller by the given amount.

The method can be called in four different ways (similar to how margins and padding is specified in css files):

  • box.pad(all)
  • box.pad(vert, hor)
  • box.pad(top, hor, bottom)
  • box.pad(top, right, bottom, left)

Simple example

>>> from boxes import *
>>> ctx = Context()
>>> outer_box = ctx.box(width=10)
>>> inner_box = ctx.box(height=4)
>>> outer_box.pad(0.5).fix(inner_box)
>>> outer_box.solve()
>>> print(outer_box.loc, outer_box.size)
(0.0, 0.0) (10.0, 5.0)
>>> print(inner_box.loc, inner_box.size)
(0.5, 0.5) (9.0, 4.0)

Center a box inside another

>>> from boxes import *
>>> ctx = Context()
>>> outer_box = ctx.box(size=(6, 5))
>>> inner_box = ctx.box(size=(4, 4))
>>> outer_box.pad(ctx.sym(), ctx.sym()).fix(inner_box)
>>> outer_box.solve()
>>> print(outer_box.loc, outer_box.size)
(0.0, 0.0) (6.0, 5.0)
>>> print(inner_box.loc, inner_box.size)
(1.0, 0.5) (4.0, 4.0)
surround(*args)

Similar to pad(), but the arguments are negated.

Properties

rect

The position of the box as a boxes.cartesian.Rect instance. The dimensions of rect are expressions of type symmath.expr.Expr until you solve() the context.

width, height, top, right, bottom, left, loc, and size

Any property of rect is also made a property of this class.

context

The boxes.context.Context of this box.