Skip to content

Rule

A rule widget to separate content, similar to a <hr> HTML tag.

  • Focusable
  • Container

Examples

Horizontal Rule

The default orientation of a rule is horizontal.

The example below shows horizontal rules with all the available line styles.

HorizontalRulesApp                         solid (default)                          ────────────────────────────────────────────────────────────────                              heavy                               ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━                              thick                               ████████████████████████████████████████████████████████████████                              dashed                              ╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍                              double                              ════════════════════════════════════════════════════════════════                              ascii                               ----------------------------------------------------------------

from textual.app import App, ComposeResult
from textual.containers import Vertical
from textual.widgets import Label, Rule


class HorizontalRulesApp(App):
    CSS_PATH = "horizontal_rules.tcss"

    def compose(self) -> ComposeResult:
        with Vertical():
            yield Label("solid (default)")
            yield Rule()
            yield Label("heavy")
            yield Rule(line_style="heavy")
            yield Label("thick")
            yield Rule(line_style="thick")
            yield Label("dashed")
            yield Rule(line_style="dashed")
            yield Label("double")
            yield Rule(line_style="double")
            yield Label("ascii")
            yield Rule(line_style="ascii")


if __name__ == "__main__":
    app = HorizontalRulesApp()
    app.run()
Screen {
    align: center middle;
}

Vertical {
    height: auto;
    width: 80%;
}

Label {
    width: 100%;
    text-align: center;
}

Vertical Rule

The example below shows vertical rules with all the available line styles.

VerticalRulesApp solid heavy thick dasheddoubleascii | | | | | | | | | | | | | | | | | | |

from textual.app import App, ComposeResult
from textual.containers import Horizontal
from textual.widgets import Label, Rule


class VerticalRulesApp(App):
    CSS_PATH = "vertical_rules.tcss"

    def compose(self) -> ComposeResult:
        with Horizontal():
            yield Label("solid")
            yield Rule(orientation="vertical")
            yield Label("heavy")
            yield Rule(orientation="vertical", line_style="heavy")
            yield Label("thick")
            yield Rule(orientation="vertical", line_style="thick")
            yield Label("dashed")
            yield Rule(orientation="vertical", line_style="dashed")
            yield Label("double")
            yield Rule(orientation="vertical", line_style="double")
            yield Label("ascii")
            yield Rule(orientation="vertical", line_style="ascii")


if __name__ == "__main__":
    app = VerticalRulesApp()
    app.run()
Screen {
    align: center middle;
}

Horizontal {
    width: auto;
    height: 80%;
}

Label {
    width: 6;
    height: 100%;
    text-align: center;
}

Reactive Attributes

Name Type Default Description
orientation RuleOrientation "horizontal" The orientation of the rule.
line_style LineStyle "solid" The line style of the rule.

Messages

This widget sends no messages.

Bindings

This widget has no bindings.

Component Classes

This widget has no component classes.


textual.widgets.Rule class

def __init__(
    self,
    orientation="horizontal",
    line_style="solid",
    *,
    name=None,
    id=None,
    classes=None,
    disabled=False
):

Bases: Widget

A rule widget to separate content, similar to a <hr> HTML tag.

Parameters
Parameter Default Description
orientation
RuleOrientation
'horizontal'

The orientation of the rule.

line_style
LineStyle
'solid'

The line style of the rule.

name
str | None
None

The name of the widget.

id
str | None
None

The ID of the widget in the DOM.

classes
str | None
None

The CSS classes of the widget.

disabled
bool
False

Whether the widget is disabled or not.

line_style class-attribute instance-attribute

line_style: Reactive[LineStyle] = line_style

The line style of the rule.

orientation class-attribute instance-attribute

orientation: Reactive[RuleOrientation] = orientation

The orientation of the rule.

horizontal classmethod

def horizontal(
    cls,
    line_style="solid",
    name=None,
    id=None,
    classes=None,
    disabled=False,
):

Utility constructor for creating a horizontal rule.

Parameters
Parameter Default Description
line_style
LineStyle
'solid'

The line style of the rule.

name
str | None
None

The name of the widget.

id
str | None
None

The ID of the widget in the DOM.

classes
str | None
None

The CSS classes of the widget.

disabled
bool
False

Whether the widget is disabled or not.

Returns
Type Description
Rule

A rule widget with horizontal orientation.

vertical classmethod

def vertical(
    cls,
    line_style="solid",
    name=None,
    id=None,
    classes=None,
    disabled=False,
):

Utility constructor for creating a vertical rule.

Parameters
Parameter Default Description
line_style
LineStyle
'solid'

The line style of the rule.

name
str | None
None

The name of the widget.

id
str | None
None

The ID of the widget in the DOM.

classes
str | None
None

The CSS classes of the widget.

disabled
bool
False

Whether the widget is disabled or not.

Returns
Type Description
Rule

A rule widget with vertical orientation.

textual.widgets.rule

LineStyle module-attribute

LineStyle = Literal[
    "ascii",
    "blank",
    "dashed",
    "double",
    "heavy",
    "hidden",
    "none",
    "solid",
    "thick",
]

The valid line styles of the rule widget.

RuleOrientation module-attribute

RuleOrientation = Literal['horizontal', 'vertical']

The valid orientations of the rule widget.

InvalidLineStyle class

Bases: Exception

Exception raised for an invalid rule line style.

InvalidRuleOrientation class

Bases: Exception

Exception raised for an invalid rule orientation.