欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python?Textual文本用戶界面庫使用原理探索

 更新時間:2024年02月01日 09:48:41   作者:菲宇?Python與Django學(xué)習(xí)  
這篇文章主要為大家介紹了Python?Textual文本用戶界面框架使用原理探索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

Python Textual文本用戶界面庫

Textual是一個用于 Python 的 TUI(文本用戶界面)庫,是 Rich 的姐妹項(xiàng)目,也依賴于 Rich。它支持 Rich 的 Renderable 類,同時有自己的互動性組件 Widget 類。通過使用簡單的 Python API 構(gòu)建復(fù)雜的用戶界面,在shell工具或?yàn)g覽器上運(yùn)行。

Textual 可在 Linux、macOS 和 Windows 上運(yùn)行。

Textual 需要 Python 3.7 或更高版本。

Textual 原理

  • Textual 使用 Rich 來渲染富文本,所以 Rich 可以渲染的任何東西都可以在 Textual 中使用。
  • Textual 的事件處理是異步的(使用 async 和 await 關(guān)鍵字)。Widgets(UI 組件)可以獨(dú)立地更新,并通過消息傳遞相互溝通。
  • Textual 與現(xiàn)代 Web 開發(fā)有更多的共同點(diǎn),布局是用 CSS 完成的,主題可以用 CSS 定制。其他技術(shù)是借用了 JS 框架,如 Vue 和 Reactive。

安裝 Textual

pip install textual

如果計劃開發(fā)文本應(yīng)用,還應(yīng)使用以下命令安裝開發(fā)工具:

pip install textual-dev

安裝 Textual 后,運(yùn)行以下命令以了解它的功能:

python -m textual

運(yùn)行結(jié)果:

widgets小部件

Button #按鈕
Checkbox #復(fù)選框
Collapsible #收起/折疊
ContentSwitcher#內(nèi)容切換器
DataTable#數(shù)據(jù)表
Digits #數(shù)字
DirectoryTree #目錄樹
Footer #頁腳
Header#頁眉
Input#輸入
Label#標(biāo)簽
ListView#列表
LoadingIndicator#加載指示器
Log#日志
MarkdownViewer#Markdown查看器
Markdown#Markdown
OptionList#選項(xiàng)列表
Placeholder#占位符
Pretty#格式化
ProgressBar#進(jìn)度條
RadioButton#單選按鈕
RadioSet#復(fù)選按鈕
RichLog#rich日志
Rule#分割線
Select#選擇
SelectionList#選擇列表
Sparkline#柱狀圖
Static#靜態(tài)文件
Switch#開關(guān)
Tabs#
TabbedContent選項(xiàng)卡內(nèi)容
TextArea#文本區(qū)域
Tree#樹

Textual 使用 CSS 將樣式應(yīng)用于小部件

倒計時示例

from time import monotonic
from textual.app import App, ComposeResult
from textual.containers import ScrollableContainer
from textual.reactive import reactive
from textual.widgets import Button, Footer, Header, Static
class TimeDisplay(Static):
    """A widget to display elapsed time."""
    start_time = reactive(monotonic)
    time = reactive(0.0)
    total = reactive(0.0)
    def on_mount(self) -> None:
        """Event handler called when widget is added to the app."""
        self.update_timer = self.set_interval(1 / 60, self.update_time, pause=True)
    def update_time(self) -> None:
        """Method to update time to current."""
        self.time = self.total + (monotonic() - self.start_time)
    def watch_time(self, time: float) -> None:
        """Called when the time attribute changes."""
        minutes, seconds = divmod(time, 60)
        hours, minutes = divmod(minutes, 60)
        self.update(f"{hours:02,.0f}:{minutes:02.0f}:{seconds:05.2f}")
    def start(self) -> None:
        """Method to start (or resume) time updating."""
        self.start_time = monotonic()
        self.update_timer.resume()
    def stop(self) -> None:
        """Method to stop the time display updating."""
        self.update_timer.pause()
        self.total += monotonic() - self.start_time
        self.time = self.total
    def reset(self) -> None:
        """Method to reset the time display to zero."""
        self.total = 0
        self.time = 0
class Stopwatch(Static):
    """A stopwatch widget."""
    def on_button_pressed(self, event: Button.Pressed) -> None:
        """Event handler called when a button is pressed."""
        button_id = event.button.id
        time_display = self.query_one(TimeDisplay)
        if button_id == "start":
            time_display.start()
            self.add_class("started")
        elif button_id == "stop":
            time_display.stop()
            self.remove_class("started")
        elif button_id == "reset":
            time_display.reset()
    def compose(self) -> ComposeResult:
        """Create child widgets of a stopwatch."""
        yield Button("Start", id="start", variant="success")
        yield Button("Stop", id="stop", variant="error")
        yield Button("Reset", id="reset")
        yield TimeDisplay()
class StopwatchApp(App):
    """A Textual app to manage stopwatches."""
    CSS_PATH = "test3.tcss"
    BINDINGS = [("d", "toggle_dark", "Toggle dark mode")]
    def compose(self) -> ComposeResult:
        """Called to add widgets to the app."""
        yield Header()
        yield Footer()
        yield ScrollableContainer(Stopwatch(), Stopwatch(), Stopwatch())
    def action_toggle_dark(self) -> None:
        """An action to toggle dark mode."""
        self.dark = not self.dark
if __name__ == "__main__":
    app = StopwatchApp()
    app.run()

css樣式

Stopwatch {
    layout: horizontal;
    background: $boost;
    height: 5;
    margin: 1;
    min-width: 50;
    padding: 1;
}
TimeDisplay {
    content-align: center middle;
    text-opacity: 60%;
    height: 3;
}
Button {
    width: 16;
}
#start {
    dock: left;
}
#stop {
    dock: left;
    display: none;
}
#reset {
    dock: right;
}
.started {
    text-style: bold;
    background: $success;
    color: $text;
}
.started TimeDisplay {
    text-opacity: 100%;
}
.started #start {
    display: none
}
.started #stop {
    display: block
}
.started #reset {
    visibility: hidden
}

運(yùn)行效果

其他示例可以參考github

參考文檔:

項(xiàng)目github: https://github.com/Textualize/textual 

官網(wǎng)文檔: https://textual.textualize.io/ 

以上就是Python Textual文本用戶界面庫使用原理探索的詳細(xì)內(nèi)容,更多關(guān)于Python Textual庫的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • python中創(chuàng)建和修改yaml文件的方法

    python中創(chuàng)建和修改yaml文件的方法

    YAML 是 "YAML Ain’t a Markup Language"的遞歸縮寫,yaml簡潔美觀,是一種常用的標(biāo)記語言,可以用來表達(dá)多種數(shù)據(jù)結(jié)構(gòu)和配置文件,本文給大家介紹python中如何創(chuàng)建和修改yaml文件,感興趣的朋友一起看看吧
    2023-11-11
  • Python使用Selenium模塊模擬瀏覽器抓取斗魚直播間信息示例

    Python使用Selenium模塊模擬瀏覽器抓取斗魚直播間信息示例

    這篇文章主要介紹了Python使用Selenium模塊模擬瀏覽器抓取斗魚直播間信息,涉及Python基于Selenium模塊的模擬瀏覽器登陸、解析、抓取信息,以及MongoDB數(shù)據(jù)庫的連接、寫入等相關(guān)操作技巧,需要的朋友可以參考下
    2018-07-07
  • pytorch-gpu安裝的經(jīng)驗(yàn)與教訓(xùn)

    pytorch-gpu安裝的經(jīng)驗(yàn)與教訓(xùn)

    本文主要介紹了pytorch-gpu安裝的經(jīng)驗(yàn)與教訓(xùn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2023-01-01
  • Python將一個Excel拆分為多個Excel

    Python將一個Excel拆分為多個Excel

    這篇文章主要為大家詳細(xì)介紹了Python將一個Excel拆分為多個Excel,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • 最新評論