[Python] Textual Python "input-Submitted" question

Discussão em 'Python' iniciado por Stack, Abril 16, 2025.

  1. Stack

    Stack Membro Participativo

    I'm using textualize for a small program and in it I have two @on(Input.Submitted) that point to each one to a different id, but when I return the submitted input I only receive the return of one of the inputs, what could I be doing wrong?

    from rich import print
    from textual import on
    from textual.app import App, ComposeResult
    from textual.binding import Binding
    from textual.widgets import Input, Label, RichLog
    from textual.validation import ValidationResult, Validator
    from datetime import datetime

    #seguradora
    class ValidaSeguradora(Validator):
    def validate(self, value: str) -> ValidationResult:
    if not value in ["06238", "05665"]:
    return self.failure(f'Seguradora {value} errada, verfique!')

    return self.success()

    #AnoMes
    class ValidaAnoMes(Validator):
    def validate(self, value: str) -> ValidationResult:
    try:
    datetime.strptime(value, '%Y%m')
    return self.success()
    except ValueError:
    return self.failure(f"Ano Mês de Referência, {value}, inválido!")

    class AutomateMain(App[None]):
    AUTO_FOCUS = None

    BINDINGS = [
    Binding(key="q", action="quit", description="Exit App"),
    ]

    def on_mount(self) -> None:
    self.theme = "gruvbox"
    self.title = "TEST"
    self.sub_title = "TEST"

    def compose(self) -> ComposeResult:
    yield Label("Enter the connection parameter (00000 ou 00001):")
    yield Input(id="inputseguradora",
    placeholder="seguradora",
    validate_on=["submitted"],
    valid_empty=True,
    )

    yield Label("Enter the year month of reference (AAAAMM):")
    yield Input(id="inputanomes",
    placeholder="Year Month",
    validate_on=["submitted"],
    valid_empty=True,
    )
    yield Label("Logs:")
    yield RichLog()

    @on(Input.Submitted, "#inputseguradora")
    def handle_inputseguradora(self, event: Input.Submitted) -> None:
    rich_log = self.query_one(RichLog)
    rich_log.write(self.query_one(Input).id)
    rich_log.write(self.query_one(Input).value)

    @on(Input.Submitted, "#inputanomes")
    def handle_inputanomes(self, event: Input.Submitted) -> None:
    rich_log = self.query_one(RichLog)
    rich_log.write(self.query_one(Input).id)
    rich_log.write(self.query_one(Input).value)

    if __name__ == "__main__":
    app = AutomateMain()
    print(app.run())


    capture @on(Input.Submitted, "#id") events correctly

    Continue reading...

Compartilhe esta Página