python 文本:更改值后UI未更新 您所在的位置:网站首页 flutter富文本编辑器 python 文本:更改值后UI未更新

python 文本:更改值后UI未更新

2023-03-17 22:20| 来源: 网络整理| 查看: 265

我正在尝试使用Python的textual包构建一个简单的基于TUI的应用程序。我有一个左面板,我想在那里显示项目列表,在右面板上,我想显示从左面板中选择的项目的详细信息。因此,我想使用文本库提供的键绑定在左面板中添加项目,但当我向列表添加新项目时,它不会更新左面板的UI以显示新添加的项目列表中的项。我正在关注此[doc][1][1]: https://textual.textualize.io/guide/reactivity/#__tabbed_2_1添加该功能,但它没有按预期工作。我无法找出这里的错误或我对事物的理解不正确。下面是我的完整代码:

import uuid from time import monotonic from textual.app import App, ComposeResult from textual.containers import Container from textual.reactive import reactive from textual.widget import Widget from textual.widgets import Button, Header, Footer, Static, ListView, ListItem, Label class LeftPanel(Widget): items = reactive([]) def compose(self) -> ComposeResult: yield Static( "All Request", expand=True, id="left_panel_header" ) yield ListView( *self.items, initial_index=None, ) class RightPanel(Widget): """A stopwatch widget.""" def compose(self) -> ComposeResult: yield ListView( ListItem(Label("4")), ListItem(Label("5")), ListItem(Label("6")), initial_index=None, ) class DebugApp(App): """A Textual app to manage stopwatches.""" CSS_PATH = "main.css" BINDINGS = [ ("d", "toggle_dark", "Toggle dark mode"), ("a", "add_item", "Add new item"), ] def compose(self) -> ComposeResult: """Called to add widgets to the app.""" yield Container(LeftPanel(id="my_list"), id="left_panel") yield Container(RightPanel(), id="right_panel") yield Footer() def action_add_item(self): self.query_one("#my_list").items.append(ListItem(Label(str(uuid.uuid4()), classes="request_item"))) self.dark = not self.dark # This works def action_toggle_dark(self) -> None: """An action to toggle dark mode.""" self.dark = not self.dark def render_ui(): app = DebugApp(watch_css=True) app.run()


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有