Skip to content

Connecting a click event on a widget to a function. #3680

Answered by davep
JoeZiminski asked this question in Q&A
Discussion options

You must be logged in to vote

If it's a specific widget you want to do this for, you could subclass that widget and handle it from there, adding your own Clicked message. For example:

from __future__ import annotations
from dataclasses import dataclass

from textual import on
from textual.app import App, ComposeResult
from textual.events import Click
from textual.message import Message
from textual.widgets import Input, Log

class ClickableInput(Input):

    @dataclass
    class Clicked(Message):
        input: ClickableInput

    def _on_click(self, _: Click) -> None:
        self.post_message(self.Clicked(self))


class InputClickApp(App[None]):

    def compose(self) -> ComposeResult:
        yield ClickableInput()…

Replies: 3 comments 2 replies

Comment options

You must be logged in to vote
0 replies
Answer selected by JoeZiminski
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@davep
Comment options

@JoeZiminski
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #3679 on November 14, 2023 19:58.