是否可以使用 Python Rich在Layout 元素中使用Prompt 获取用户输入?
我的目标是使用 Rich 的 Layout 构建一个有 4 个窗格的全屏窗口。包含标题、成分和方法的前 3 个工作正常,但我希望底部包含一个用户输入提示。
期望的输出:
用户输入的文本显示在布局的底部面板内。
┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ │
│ Chocolate cheesecake │
│ │
└───────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
┌──────────────── 'ingredients' (58 x 7) ────────────────┐┌─────────────────── 'method' (59 x 7) ───────────────────┐
│ ││ │
│ ││ │
│ Layout(name='ingredients') ││ Layout(name='method') │
│ ││ │
│ ││ │
└────────────────────────────────────────────────────────┘└─────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────── Search for a recipe ───────────────────────────────────────────────┐
│ │
│ > : │
│ │
└───────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
我的尝试:
from rich import print
from rich.panel import Panel
from rich.layout import Layout
from rich.prompt import Prompt
def rich_ui():
while True:
layout = Layout()
layout.split_column(
Layout(name="banner"),
Layout(name="recipe"),
Layout(name="search")
)
layout['banner'].update(Panel('Chocolate cheesecake', padding=1))
layout['banner'].size = 5
layout['recipe'].split_row(
Layout(name="ingredients"),
Layout(name="method")
)
layout['search'].update(Panel(Prompt.ask('> '), title='Search for a recipe'))
layout['search'].size = 5
print(layout)
if __name__ == '__main__':
rich_ui()
实际输出:
请注意,提示>:
位于布局部分之外。
┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ │
│ Chocolate cheesecake │
│ │
└───────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
┌──────────────── 'ingredients' (58 x 7) ────────────────┐┌─────────────────── 'method' (59 x 7) ───────────────────┐
│ ││ │
│ ││ │
│ Layout(name='ingredients') ││ Layout(name='method') │
│ ││ │
│ ││ │
└────────────────────────────────────────────────────────┘└─────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────── Search for a recipe ───────────────────────────────────────────────┐
│ │
│ │
│ │
└───────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
> :