Textual是一个Python的终端库,网址:https://github.com/Textualize/textual,可用于创建精美复杂的终端应用。

特征

想法:终端刷论坛、终端看B站

入门01 HelloWorld

from textual.app import App
from textual.widgets import Label  # 引入一个控件类型

class HelloWorldApp(App):
    def compose(self):
				# yield 添加组件
        yield Label("Hello Textual", id="text")

if __name__ == "__main__":
    app = HelloWorldApp(css_path="app.css", watch_css=True) # 监视CSS标签
    app.run()
Screen {
    color: black;
}
Label {
	background: blue;
	width: 100%;
	text-align: center;
}
#text_chinese {
	background: green;
}

入门02 切换窗口

from textual.app import App
from textual.screen import Screen
from textual.widgets import Label  # 引入一个控件类型

class AuthorApp(Screen):
    def compose(self):
        yield Label("Author: Coder-Liuu")
        yield Label("版权所有,盗版必究")

class HelloWorldApp(App):

    # 键位绑定
    BINDINGS = [
				# 按键  调用的方法(实际方法前面加上action) 说明
        ("j", "show_info", "Author Inof:)"),
        ("k", "pop_info", "Author Inof:)")
    ]

    def compose(self):
        yield Label("Hello Textual")
        yield Label("世界 你好", id="text")

		# push到最顶层
    def action_show_info(self):
        self.push_screen(AuthorApp())

    # 弹出顶层
    def action_pop_info(self):
        self.pop_screen()

if __name__ == "__main__":
    app = HelloWorldApp(css_path="app.css", watch_css=True)
    app.run()
Screen {
    align: center middle;
    background: red;
}

Label {
    color: white;
}

#text {
    color: white;
}

AuthorApp {
    color: red;
    background: blue;
}