kitao/pyxel · error · ValueError

width or height must be specified, but not both

Error message

width or height must be specified, but not both

What it means

Pyxel's ScrollBar widget requires exactly one of `width` or `height`: width implies a horizontal scroll bar, height a vertical one (the other dimension is hardcoded to 7). Passing neither, or both, is ambiguous, so the constructor raises ValueError immediately.

Source

Thrown at python/pyxel/editor/widgets/scroll_bar.py:31

    # Events:
    #   change (value)

    def __init__(
        self,
        parent,
        x,
        y,
        *,
        width=None,
        height=None,
        scroll_amount,
        slider_amount,
        value,
        with_shadow=True,
        **kwargs,
    ):
        if (width is None) == (height is None):
            raise ValueError("width or height must be specified, but not both")

        if height is not None:
            width = 7
            self._is_vertical = True
        else:
            height = 7
            self._is_vertical = False

        super().__init__(parent, x, y, width, height, **kwargs)
        self.scroll_amount = scroll_amount
        self.slider_amount = slider_amount
        self._with_shadow = with_shadow
        self._drag_offset = 0
        self._is_dragged = False

        self.new_var("value_var", value)
        self.add_var_event_listener("value_var", "set", self.__on_value_set)
        self.add_var_event_listener("value_var", "change", self.__on_value_change)

View on GitHub (pinned to 50f9bd7778)

Solutions

  1. Pass exactly one dimension: `width=N` for a horizontal bar, `height=N` for a vertical bar.
  2. If computing dimensions dynamically, ensure only one is ever set (e.g. pass the other as None explicitly).
  3. If a scrollbar direction is configurable, branch before construction and build kwargs with only the relevant dimension.

Example fix

// before
bar = ScrollBar(x=0, y=0, width=100, height=100, slider_amount=10, value=0)
// after
bar = ScrollBar(x=0, y=0, width=100, height=None, slider_amount=10, value=0)  # horizontal
Defensive patterns

Strategy: validation

Validate before calling

def make_scrollbar(x, y, width=None, height=None, **kwargs):
    if (width is None) == (height is None):
        raise ValueError("specify exactly one of width (horizontal) or height (vertical)")
    return ScrollBar(x=x, y=y, width=width, height=height, **kwargs)

Type guard

def scrollbar_dims_ok(width, height):
    return (width is None) != (height is None)

Prevention

When it happens

Trigger: Calling ScrollBar(...) with width=None and height=None, or with both width and height set. Any keyword misuse like ScrollBar(width=10, height=10) or omitting both dimensions in the editor widget config.

Common situations: Building a custom editor layout and copying constructor args between horizontal and vertical bars without removing the other dimension; dynamically computing width/height where both may end up non-None; forgetting to pass a dimension when the widget is created from kwargs.


AI-assisted analysis of kitao/pyxel@50f9bd7778 (2026-09-03). Data as JSON: /api/errors/f5c91586f7788d8b. Report an issue: GitHub.