问题描述
无论每个窗口(全屏应用程序)中内容的宽度如何,我都想使用prompt_toolkit创建一个均匀分布的垂直布局。不良行为-在一个或多个控件中更改内容时,会重新计算布局以适应更大或更窄的动态内容。
有没有一种方法可以使给定屏幕尺寸的布局静态化;也就是说,仅在初始化或调整大小时才渲染窗口,以使布局列均匀分布吗?
下面的示例代码(按c
在任一列上插入随机长度的内容,布局宽度会发生变化)。即使添加用户消息,也可能导致在足够狭窄的终端上初始化宽度不均。
from random import randint
from prompt_toolkit.application import Application
from prompt_toolkit.key_binding import KeyBindings
from prompt_toolkit.layout.containers import VSplit,Window
from prompt_toolkit.layout.controls import FormattedTextControl
from prompt_toolkit.layout.layout import Layout
user_msg = "press 'c' to change,'q' to quit"
body = VSplit(
[
Window(FormattedTextControl(text=user_msg)),Window(width=1,char="|"),Window(FormattedTextControl()),]
)
kb = KeyBindings()
@kb.add("c")
def change_content(event):
for w in event.app.layout.find_all_windows():
prev_width = f"prev_width: {w.render_info.window_width}"
rand_str = "*" * randint(1,50)
w.content.text = "\n".join([prev_width,rand_str])
@kb.add("q")
def quit(event):
event.app.exit()
layout = Layout(body)
app = Application(layout=layout,key_bindings=kb,full_screen=True)
app.run()
解决方法
传递参数 ignore_content_width 有效。
body = VSplit(
[
Window(FormattedTextControl(text=user_msg),ignore_content_width=True),Window(width=1,char="|"),Window(FormattedTextControl(),]
)