Skip to content

Anchor layout

Places children at exact positions, the way a visual form designer does, and says how each one follows a resize: pz.anchor(child, x=, y=, width=, height=, anchor=) keeps the distance to the edges it is anchored to, stretches between opposite edges and stays centered otherwise. Presets such as "top_right", "bottom_wide", "center" or "full" cover the usual cases. pyzed.anchors.place is the math behind it, for your own layouts.

pz.anchor_layout · Category: Layout · Full reference →

Anchor layoutAnchor layout
  • For forms drawn in Pyzed Design (it is what the Form view generates) or ported from Visual Basic, Qt Designer or WinForms.
  • For game HUDs and overlays where things stick to corners.
  • For forms that grow and shrink with the window, prefer row, column and grid.
import pyzed as pz
from pyzed import ui
class Report(pz.App):
def view(self):
return pz.anchor_layout([
pz.anchor(ui.heading("Report"), x=16, y=12, width=368, height=28, anchor="top_wide"),
pz.anchor(self.table(), x=16, y=48, width=368, height=196, anchor="full"),
pz.anchor(ui.button("Close", self.close), x=284, y=252, width=100, height=32,
anchor="bottom_right"),
], design=(400, 300))

Each layout was designed at 110 × 44 and drawn at 260 × 110: the badge keeps its distance to the edges it is anchored to.

Anchor layout: top_leftAnchor layout: top_left
top_left
Code
t = pz.theme.current()
pz.container(pz.anchor_layout([pz.anchor(ui.badge("top_left"), x=10, y=10, width=90, height=24,
anchor="top_left")], design=(110, 44)),
width=260, height=110, border_color=t.border, border_width=1, radius=8)
Anchor layout: top_rightAnchor layout: top_right
top_right
Code
t = pz.theme.current()
pz.container(pz.anchor_layout([pz.anchor(ui.badge("top_right"), x=10, y=10, width=90, height=24,
anchor="top_right")], design=(110, 44)),
width=260, height=110, border_color=t.border, border_width=1, radius=8)
Anchor layout: bottom_leftAnchor layout: bottom_left
bottom_left
Code
t = pz.theme.current()
pz.container(pz.anchor_layout([pz.anchor(ui.badge("bottom_left"), x=10, y=10, width=90, height=24,
anchor="bottom_left")], design=(110, 44)),
width=260, height=110, border_color=t.border, border_width=1, radius=8)
Anchor layout: bottom_rightAnchor layout: bottom_right
bottom_right
Code
t = pz.theme.current()
pz.container(pz.anchor_layout([pz.anchor(ui.badge("bottom_right"), x=10, y=10, width=90, height=24,
anchor="bottom_right")], design=(110, 44)),
width=260, height=110, border_color=t.border, border_width=1, radius=8)
Anchor layout: centerAnchor layout: center
center
Code
t = pz.theme.current()
pz.container(pz.anchor_layout([pz.anchor(ui.badge("center"), x=10, y=10, width=90, height=24,
anchor="center")], design=(110, 44)),
width=260, height=110, border_color=t.border, border_width=1, radius=8)
Anchor layout: top_wideAnchor layout: top_wide
top_wide
Code
t = pz.theme.current()
pz.container(pz.anchor_layout([pz.anchor(pz.container(width="fill", height="fill", bg=t.accent, radius=4),
x=10, y=10, width=90, height=24, anchor="top_wide")], design=(110, 44)),
width=260, height=110, border_color=t.border, border_width=1, radius=8)
Anchor layout: bottom_wideAnchor layout: bottom_wide
bottom_wide
Code
t = pz.theme.current()
pz.container(pz.anchor_layout([pz.anchor(pz.container(width="fill", height="fill", bg=t.accent, radius=4),
x=10, y=10, width=90, height=24, anchor="bottom_wide")], design=(110, 44)),
width=260, height=110, border_color=t.border, border_width=1, radius=8)
Anchor layout: left_tallAnchor layout: left_tall
left_tall
Code
t = pz.theme.current()
pz.container(pz.anchor_layout([pz.anchor(pz.container(width="fill", height="fill", bg=t.accent, radius=4),
x=10, y=10, width=90, height=24, anchor="left_tall")], design=(110, 44)),
width=260, height=110, border_color=t.border, border_width=1, radius=8)
Anchor layout: center_wideAnchor layout: center_wide
center_wide
Code
t = pz.theme.current()
pz.container(pz.anchor_layout([pz.anchor(pz.container(width="fill", height="fill", bg=t.accent, radius=4),
x=10, y=10, width=90, height=24, anchor="center_wide")], design=(110, 44)),
width=260, height=110, border_color=t.border, border_width=1, radius=8)
Anchor layout: fullAnchor layout: full
full
Code
t = pz.theme.current()
pz.container(pz.anchor_layout([pz.anchor(pz.container(width="fill", height="fill", bg=t.accent, radius=4),
x=10, y=10, width=90, height=24, anchor="full")], design=(110, 44)),
width=260, height=110, border_color=t.border, border_width=1, radius=8)

Anchor layout has no events of its own: it shows what you give it. Like every widget it also has the 18 common events (click, double_click, right_click, mouse_enter, drag, key_down, resize…), attached with .on(...): see Events and Events of every component.

anchor_layout(children, *, design=None, width='fill', height='fill')

Places children made with pz.anchor freely, like a designed form: when it is resized, each child keeps its distance to the edges it is anchored to, stretches between opposite ones and stays centered otherwise. Later children are drawn on top. See Anchored layouts.

Property Default Description
children required Children made with pz.anchor(...) (None is skipped).
design None (width, height) the children were laid out in; by default the smallest size that holds them.
width "fill" Width.
height "fill" Height.

Full reference →

anchor(
content,
*,
x=0,
y=0,
width=100,
height=28,
anchor='top_left',
min_width=0,
min_height=0,
max_width=0,
max_height=0,
pivot=(0.5, 0.5),
rotate=0,
)

Places a child of pz.anchor_layout at x, y with width and height (in the layout’s design size) and says how it follows a resize.

Property Default Description
content required The widget.
x 0 Left edge in the design size.
y 0 Top edge in the design size.
width 100 Width in the design size.
height 28 Height in the design size.
anchor "top_left" A preset of pyzed.anchors.PRESETS ("top_right", "bottom_wide", "center", "full"…), words ("left right bottom") or a tuple of edges.
min_width 0 Smallest width when it stretches (0: no limit).
min_height 0 Smallest height when it stretches.
max_width 0 Largest width when it stretches (0: no limit).
max_height 0 Largest height when it stretches.
pivot (0.5, 0.5) (x, y) from 0 to 1: the point that stays put when a limit stops it, and that rotate turns around.
rotate 0 Degrees clockwise.

Full reference →

place(
rect,
anchor,
design,
actual,
*,
min_size=(0, 0),
max_size=(0, 0),
pivot=(0.5, 0.5),
)

Where a box designed in a parent of size design goes in a parent of size actual, following its anchor, with size limits and a pivot. The math behind pz.anchor_layout and designed forms.

Property Default Description
rect required (x, y, width, height) in the design size.
anchor required A preset, words or edges.
design required The parent’s design size.
actual required The parent’s current size.
min_size (0, 0) (width, height) lower limits (0: none).
max_size (0, 0) (width, height) upper limits (0: none).
pivot (0.5, 0.5) The point that stays put when a limit stops the box from stretching.

Full reference →