Textual,我有一个应用程序,有3种模式.但是,我想根据用户输入切换屏幕内的模式.我怎么才能做到这一点呢? App.py:

class DemoApp(App[None]):
MODES = {
        "profile": ProfileScreen,
        "chat": ChatScreen,
        "help": HelpScreen,
        }
    def on_mount(self) -> None:
        """On running"""
        LOGGER.info("Starting")
        self.switch_mode("chat")
class ChatScreen(Screen):
    def compose(self) -> ComposeResult:
        yield Button(label="Profile", variant="success", id="change_profile")

    async def on_button_pressed(self) -> None:
        await self.process()

    async def process():
        switch_mode("profile")  # How do I do this?

我在谷歌上搜索了所有内容,并阅读了相关文档.但我还是找不到解决办法.

推荐答案

您可以通过app property获取正在运行的App实例,所以只需:

async def process(self):
    self.app.switch_mode("profile")

请注意,您忘记了process实例方法中的self参数.


一个可重复的例子:

from textual.app import App, ComposeResult
from textual.screen import Screen
from textual.widgets import Button


class ChatScreen(Screen):
    def compose(self) -> ComposeResult:
        yield Button(label="Profile", variant="success", id="change_profile")

    async def on_button_pressed(self) -> None:
        self.app.switch_mode("profile")


class ProfileScreen(Screen):
    def compose(self) -> ComposeResult:
        yield Button(label="Help", variant="success", id="change_profile")

    async def on_button_pressed(self) -> None:
        self.app.switch_mode("help")


class HelpScreen(Screen):
    def compose(self) -> ComposeResult:
        yield Button(label="Chat", variant="success", id="change_profile")

    async def on_button_pressed(self) -> None:
        self.app.switch_mode("chat")


class DemoApp(App[None]):
    MODES = {
            "profile": ProfileScreen,
            "chat": ChatScreen,
            "help": HelpScreen,
            }

    def on_mount(self) -> None:
        """On running"""
        # LOGGER.info("Starting")
        self.switch_mode("chat")


if __name__ == "__main__":
    app = DemoApp()
    app.run()

Python相关问答推荐

来自ARIMA结果的模型方程

Django序列化器没有验证或保存数据

将词典写入Excel

按 struct 值对Polars列表[struct[]]排序

telegram 机器人API setMyName不起作用

如何匹配3D圆柱体的轴和半径?

DuckDB将蜂巢分区插入拼花文件

使用GEKKO在简单DTE系统中进行一致初始化

使用scipy. optimate.least_squares()用可变数量的参数匹配两条曲线

如何根据参数推断对象的返回类型?

沿着数组中的轴计算真实条目

如何使用html从excel中提取条件格式规则列表?

在Python中动态计算范围

将9个3x3矩阵按特定顺序排列成9x9矩阵

cv2.matchTemplate函数匹配失败

在ubuntu上安装dlib时出错

当递归函数的返回值未绑定到变量时,非局部变量不更新:

Django—cte给出:QuerySet对象没有属性with_cte''''

如何使regex代码只适用于空的目标单元格

Python—压缩叶 map html作为邮箱附件并通过sendgrid发送