You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Textual框架下如何在屏幕间传递数据?RSS阅读器开发问题

问题:RSS阅读器中如何向新屏幕传递选中卡片的数据

我正在开发一款RSS阅读器应用,第一步是把所有Feed展示在名为ArticlesGrid的屏幕里,这个屏幕包含发布日期、来源名称和标题字段。目前焦点功能已经完成,应用运行正常,但需要增强实用性:希望用户按下回车键时,展示对应卡片的链接和完整内容等数据。我没找到类似路由或导航的可用功能,push_screen方法只能推送新屏幕,但没法携带前一屏幕的数据。请问怎么给网格中特定单元格推送数据(比如推送对应卡片的标题)?

以下是我的代码:

class ArticlesGrid(Grid):
    def compose(self) -> ComposeResult:
        for article_info in self.content:
            article = ArticleCard(article_info.get("title"))
            article.border_title = article_info.get("provider")
            article.border_subtitle = article_info.get("pub_date")
            yield article

    def handle_key(self, event: events.Key) -> bool:
        if event.key == "enter":
            focused_card = self.focus()
            if focused_card is not None:
                self.app.push_screen(...)

    @property
    def content(self):
        feeds = get_content()
        data = []
        for provider, feed in feeds.items():
            for article in feed:
                data.append(
                    {
                        "provider": provider,
                        "title": article.title,
                        "description": article.description,
                        "content": article.content,
                        "link": article.link,
                        "pub_date": article.pub_date,
                        "author": article.author,
                        "authors": article.authors,
                        "tags": article.tags,
                    }
                )
        return data

解决方法

1. 给详情屏幕添加自定义初始化参数

核心思路是把文章数据绑定到对应UI卡片上,推送新屏幕时直接将数据传入详情屏幕的构造函数:

首先创建一个详情屏幕类,接收文章数据作为初始化参数:

class ArticleDetail(Screen):
    def __init__(self, article_data, **kwargs):
        super().__init__(**kwargs)
        self.article_data = article_data

    def compose(self) -> ComposeResult:
        # 根据传入的文章数据渲染详情内容
        yield Label(f"标题: {self.article_data['title']}")
        yield Label(f"来源: {self.article_data['provider']}")
        yield Paragraph(f"完整内容: {self.article_data['content']}")
        yield Label(f"链接: {self.article_data['link']}")

然后修改ArticlesGrid的compose方法,把文章数据绑定到ArticleCard实例:

def compose(self) -> ComposeResult:
    for article_info in self.content:
        article = ArticleCard(article_info.get("title"))
        article.border_title = article_info.get("provider")
        article.border_subtitle = article_info.get("pub_date")
        # 将文章数据绑定到卡片上
        article.article_info = article_info
        yield article

最后修改handle_key方法,获取选中卡片的绑定数据并推送详情屏幕:

def handle_key(self, event: events.Key) -> bool:
    if event.key == "enter":
        focused_card = self.focus()
        if focused_card is not None:
            # 获取绑定的文章数据
            target_article = focused_card.article_info
            # 推送详情屏幕并传入数据
            self.app.push_screen(ArticleDetail(target_article))
            return True
    return super().handle_key(event)

2. 利用应用全局状态传递数据

如果你的应用有全局状态,可以将选中的文章数据存入全局变量,再在详情屏幕中读取:

修改handle_key方法:

def handle_key(self, event: events.Key) -> bool:
    if event.key == "enter":
        focused_card = self.focus()
        if focused_card is not None:
            # 将数据存入应用全局状态
            self.app.selected_article = focused_card.article_info
            self.app.push_screen(ArticleDetail())
            return True
    return super().handle_key(event)

然后在ArticleDetail中直接读取全局数据:

class ArticleDetail(Screen):
    def compose(self) -> ComposeResult:
        article_data = self.app.selected_article
        yield Label(f"标题: {article_data['title']}")
        yield Paragraph(f"完整内容: {article_data['content']}")

内容的提问来源于stack exchange,提问作者MohammedAlsahli

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 15:33:28