textual_canvas.canvas
Provides a simple character cell-based canvas widget for Textual applications.
Canvas
Canvas(
width,
height,
canvas_color=None,
pen_color=None,
name=None,
id=None,
classes=None,
disabled=False,
)
Bases: ScrollView
A simple character-cell canvas widget.
The widget is designed such that there are two 'pixels' per character cell; one being the top half of the cell, the other being the bottom. While not exactly square, this will make it more square than using a whole cell as a simple pixel.
The origin of the canvas is the top left corner.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
int
|
The width of the canvas. |
required |
|
int
|
The height of the canvas. |
required |
|
Color | None
|
An optional default colour for the canvas. |
None
|
|
Color | None
|
The optional default colour for the pen. |
None
|
|
str | None
|
The name of the canvas widget. |
None
|
|
str | None
|
The ID of the canvas widget in the DOM. |
None
|
|
str | None
|
The CSS classes of the canvas widget. |
None
|
|
bool
|
Whether the canvas widget is disabled or not. |
False
|
If canvas_color
is omitted, the widget's background
styling will
be used.
If pen_color
is omitted, the widget's color
styling will be used.
height
property
height
The height of the canvas in 'pixels'.
width
property
width
The width of the canvas in 'pixels'.
batch_refresh
batch_refresh()
A context manager that suspends all calls to refresh
until the end of the batch.
Ordinarily set_pixels
will call refresh
once it has
updated all of the pixels it has been given. Sometimes you may want
to perform a number of draw operations and having refresh
called
between each one would be inefficient given you've not drawing.
Use this context manager to batch up your drawing operations.
Example
canvas = self.query_one(Canvas)
with canvas.batch_refresh():
canvas.draw_line(10, 10, 10, 20)
canvas.draw_line(10, 20, 20, 20)
canvas.draw_line(20, 20, 20, 10)
canvas.draw_line(20, 10, 10, 10)
Note
All drawing methods have a refresh
parameter. If that is set
to True
in any of your calls those calls will still
force a refresh.
clear
Clear the canvas.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
Color | None
|
Optional default colour for the canvas. |
None
|
|
int | None
|
Optional width for the canvas. |
None
|
|
int | None
|
Optional height for the canvas. |
None
|
Returns:
Type | Description |
---|---|
Self
|
The canvas. |
If the color isn't provided, then the color used when first making the canvas is used, this in turn becomes the new default color (and will then be used for subsequent clears, unless another color is provided).
Explicitly setting the colour to None
will set the canvas
colour to whatever the widget's background
colour is.
If width
or height
are omitted then the current value for those
dimensions will be used.
clear_pixel
Clear the colour of a specific pixel on the canvas.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
int
|
The horizontal location of the pixel. |
required |
|
int
|
The vertical location of the pixel. |
required |
|
bool | None
|
Should the widget be refreshed? |
None
|
Raises:
Type | Description |
---|---|
CanvasError
|
If the pixel location is not within the canvas. |
Note
The origin of the canvas is the top left corner.
clear_pixels
Clear the colour of a collection of pixels on the canvas.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
Iterable[tuple[int, int]]
|
An iterable of tuples of x and y location. |
required |
|
bool | None
|
Should the widget be refreshed? |
None
|
Returns:
Type | Description |
---|---|
Self
|
The canvas. |
Raises:
Type | Description |
---|---|
CanvasError
|
If any pixel location is not within the canvas. |
Note
The origin of the canvas is the top left corner.
draw_circle
Draw a circle
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
int
|
The horizontal position of the center of the circle. |
required |
|
int
|
The vertical position of the center of the circle. |
required |
|
int
|
The radius of the circle. |
required |
|
Color | None
|
The colour to draw circle in. |
None
|
|
bool | None
|
Should the widget be refreshed? |
None
|
Returns:
Type | Description |
---|---|
Self
|
The canvas. |
Note
The origin of the canvas is the top left corner.
draw_line
Draw a line between two points.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
int
|
Horizontal location of the starting position. |
required |
|
int
|
Vertical location of the starting position. |
required |
|
int
|
Horizontal location of the ending position. |
required |
|
int
|
Vertical location of the ending position. |
required |
|
Color | None
|
The color to set the pixel to. |
None
|
|
bool | None
|
Should the widget be refreshed? |
None
|
Returns:
Type | Description |
---|---|
Self
|
The canvas. |
Note
The origin of the canvas is the top left corner.
draw_rectangle
Draw a rectangle.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
int
|
Horizontal location of the top left corner of the rectangle. |
required |
|
int
|
Vertical location of the top left corner of the rectangle. |
required |
|
int
|
The width of the rectangle. |
required |
|
int
|
The height of the rectangle. |
required |
|
Color | None
|
The color to draw the rectangle in. |
None
|
|
bool | None
|
Should the widget be refreshed? |
None
|
Returns:
Type | Description |
---|---|
Self
|
The canvas. |
Note
The origin of the canvas is the top left corner.
get_pixel
Get the pixel at the given location.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
int
|
The horizontal location of the pixel. |
required |
|
int
|
The vertical location of the pixel. |
required |
Returns:
Type | Description |
---|---|
Color
|
The colour of the pixel at that location. |
Raises:
Type | Description |
---|---|
CanvasError
|
If the pixel location is not within the canvas. |
Note
The origin of the canvas is the top left corner.
set_pen
set_pen(color)
set_pixel
Set the colour of a specific pixel on the canvas.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
int
|
The horizontal location of the pixel. |
required |
|
int
|
The vertical location of the pixel. |
required |
|
Color | None
|
The color to set the pixel to. |
None
|
|
bool | None
|
Should the widget be refreshed? |
None
|
Raises:
Type | Description |
---|---|
CanvasError
|
If the pixel location is not within the canvas. |
Note
The origin of the canvas is the top left corner.
set_pixels
Set the colour of a collection of pixels on the canvas.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
Iterable[tuple[int, int]]
|
An iterable of tuples of x and y location. |
required |
|
Color | None
|
The color to set the pixel to. |
None
|
|
bool | None
|
Should the widget be refreshed? |
None
|
Returns:
Type | Description |
---|---|
Self
|
The canvas. |
Raises:
Type | Description |
---|---|
CanvasError
|
If any pixel location is not within the canvas. |
Note
The origin of the canvas is the top left corner.