代码之家  ›  专栏  ›  技术社区  ›  Brad Parks

文本(python)-如何在简单的文本对象中添加点击事件?

  •  0
  • Brad Parks  · 技术社区  · 3 年前

    我正在尝试获取它,以便在由呈现的文本中添加链接 Textual

    我的文本可能有多个链接,例如:

    Hello [@click=hello]World[/] there, how are you?
    This is a test of [@click=more] more info[/] being clickable as well.
    

    在我制作的这个简单的示例中,点击单词“世界”应该会将背景颜色更改为红色,但这不起作用。

    注意:我还绑定了“b”键来做几乎相同的事情,所以我可以看到它的工作 它应该更改应用程序的背景颜色和字幕。

    import os
    import sys
    from rich.console import RenderableType
    from rich.panel import Panel
    from rich.text import Text
    from textual.app import App
    from textual.widgets import Header, Footer, ScrollView
    from textual.widgets import Placeholder
    
    class MyApp(App):
    
        async def on_load(self) -> None:
            await self.bind("b", "color('blue')")
    
        async def on_mount(self) -> None:
            await self.view.dock(Header(), size=5, edge="top")
            await self.view.dock(Footer(), edge="bottom")
            await self.view.dock(ScrollView(Panel("Hello [@click=hello]World[/] more info here")), edge="top")
    
        async def action_color(self, color:str) -> None:
            self.app.sub_title = "KEYBOARD"
            self.background = f"on {color}"
    
        async def action_hello(self) -> None:
            self.app.sub_title = "CLICKED"
            self.background = "on red"
    
    MyApp.run(title="Test click", log="textual.log")
    

    我在 textual discussions 最初 rich discussions ,但我还没能从那里收到的反馈中看到如何做到这一点,这当然很有帮助,但我在这里错过了一些东西,所以感谢您的意见。

    0 回复  |  直到 2 年前
        1
  •  2
  •   Domarm    3 年前

    看起来 @click=action 在文本中不起作用(至少我根本无法使其起作用)。
    在挖掘了丰富的文档之后,我开始学习Text类。那个,有 on method ,可以创建 click 回调。

    它支持 __add__ ,所以您可以连接多个 Text (s) 与 + 操作人员

    难题的第二部分是找出要设置的 点击 回调。我再次查看源代码,发现 _action_targets 在app.py中。它包含 {"app", "view"} 设置

    将所有内容放在一起,您可以使用 文本 具有 on(click="app.callback()") ,将调用 action_callback MyApp类的方法(文本应用程序的实例)。然后通过插入其他 文本 (s) 和链接在一起。

    下面是一个工作示例,点击Hello将背景变成红色,或者点击World将背景变成绿色。

    from rich.panel import Panel
    from rich.text import Text
    from textual.app import App
    from textual.widgets import Header, Footer, ScrollView
    
    
    class MyApp(App):
    
        async def on_load(self) -> None:
            await self.bind("b", "color('blue')")
    
        async def on_mount(self) -> None:
            await self.view.dock(Header(), size=5, edge="top")
            await self.view.dock(Footer(), edge="bottom")
            link1 = Text("Hello").on(click="app.hello()")
            link2 = Text("World").on(click="app.world()")
            panel_text = link1 + " " + link2 + Text(" more info here")
            await self.view.dock(ScrollView(Panel(panel_text)), edge="top")
    
        async def action_color(self, color: str) -> None:
            self.app.sub_title = "KEYBOARD"
            self.background = f"on {color}"
    
        async def action_hello(self) -> None:
            self.app.sub_title = "CLICKED Hello"
            self.background = "on red"
    
        async def action_world(self) -> None:
            self.app.sub_title = "CLICKED World"
            self.background = "on green"
    
    
    MyApp.run(title="Test click", log="textual.log")
    

    我知道这不是一个理想的解决方案,但这是我能得到的最接近你想要的。

        2
  •  1
  •   BrokenBenchmark Miles Budnek    2 年前

    您也可以使用制作具有不同属性的文本 Text.assemble() :

    async def on_mount(self) -> None:
        await self.view.dock(Header(), size=5, edge="top")
        await self.view.dock(Footer(), edge="bottom")
        panel_text = Text.assemble(
            "Hello ",
            Text("World","dark_blue u").on({"@click" : "app.hello()"}),
            " more ",
            Text("info","dark_blue u").on({"@click" : "app.color('blue')"}),
             " here")
        await self.view.dock(ScrollView(Panel(panel_text)), edge="top")
    
    推荐文章