Skip to content

Changing a TabbedContent does not work correctly when using a button #6406

@hellow554

Description

@hellow554

The bug

I want to switch the tab pane and found the example at https://textual.textualize.io/widgets/tabbed_content/#switching-tabs

which suggests using self.query_one(TabbedContent).active = "jessica"

Below that is an example by using Bindings. The Bindings example works fine, but using the following code does not work:

from textual import on
from textual.app import App
from textual.containers import VerticalGroup
from textual.widgets import Button, Label, TabbedContent, TabPane


class Switcher(VerticalGroup):
    def compose(self):
        yield Button("JESSICA", name="jessica")
        yield Button("PAUL", name="paul")

    @on(Button.Pressed)
    def switch_to_state(self, ev):
        self.app.query_one(TabbedContent).active = ev.button.name


class Tui(App):
    def compose(self):
        with TabbedContent():
            with TabPane("Leto", id="leto"):
                yield Switcher()
            with TabPane("Jessica", id="jessica"):
                yield Label("JESSICA")
            with TabPane("Paul", id="paul"):
                yield Label("PAUL")

one can see, that the new tab flashes for a split second, but then switches back to the original tab
It would be cool if someone could say what's wrong here and how do I fix this issue.

Thanks :)

textual diagnose

Textual Diagnostics

Versions

Name Value
Textual 8.0.2
Rich 14.3.3

Python

Name Value
Version 3.14.2
Implementation CPython
Compiler Clang 21.1.4
Executable /tmp/tmp.jX66n1cKU4/.venv/bin/python3

Operating System

Name Value
System Linux
Release 6.6.87.2-microsoft-standard-WSL2
Version #1 SMP PREEMPT_DYNAMIC Thu Jun 5 18:30:46 UTC 2025

Terminal

Name Value
Terminal Application Windows Terminal
TERM xterm-256color
COLORTERM Not set
FORCE_COLOR Not set
NO_COLOR Not set

Rich Console options

Name Value
size width=188, height=49
legacy_windows False
min_width 1
max_width 188
is_terminal True
encoding utf-8
max_height 49
justify None
overflow None
no_wrap False
highlight None
markup None
height None

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions