Dynamically change the size of a widget


I have two widgets, one to left and one to the right. If a user presses a button, I want to increase the widget on the left.

So I added an action:

async def on_load(self, event):
        await self.bind("q", "quit", "Quit")
        await self.bind("e", "extend", "Extend panel")

async def action_extend(self):
        self.left_widget.animate("layout_size", 100)

However, this causes a TypeError: can't multiply sequence by non-int of type 'float'

Is there any other way of changing the size?


Found an answer. You can’t animate it.

But, if you just set the value it changes it, too.

async def action_extend(self):
        self.left_widget.layout_size = 100

However, it would be nice if layout_size can also be animated.

Just an idea, didn’t try this, but what if you would also animate the right widget in the other direction, or make it smaller so the left widget has room to perform the animate.

I tried it again, but I can’t get it working with animation at all.

I have made a minimal example:

import os

from textual.app import App
from textual.widgets import Placeholder, Footer

class SimpleApp(App):

    resize_mode = 40

    async def on_mount(self) -> None:
        await self.view.dock(Footer(), edge="bottom")
        self.left_placeholder = Placeholder()
        self.right_placeholder = Placeholder()
        await self.view.dock(self.left_placeholder, edge="left", size=SimpleApp.resize_mode)
        await self.view.dock(self.right_placeholder, edge="right")

    async def on_load(self) -> None:
        await self.bind("r", "resize", "Resize Toggle")
        await self.bind("q", "quit", "Quit")

    async def action_resize(self) -> None:
        if SimpleApp.resize_mode == 40:
            SimpleApp.resize_mode = 100
            SimpleApp.resize_mode = 40

        # works
        self.left_placeholder.layout_size = SimpleApp.resize_mode
        self.right_placeholder.layout_size = os.get_terminal_size().columns - SimpleApp.resize_mode

        # doesn't work
        self.left_placeholder.animate("layout_size", SimpleApp.resize_mode)
        self.right_placeholder.animate("layout_size", os.get_terminal_size().columns - SimpleApp.resize_mode)

if __name__ == "__main__":

I think the layout_size value does not support animation.