如何在 python-prompt-toolkit 中向 TextArea 添加 pageup/pagedown 键绑定?

问题描述

让我们以 calculator.py 为例。

添加与鼠标滚轮一起使用的滚动条,您需要更改:

output_field = TextArea(style="class:output-field",text=help_text)

calculator.py without scrollbar

到:

output_field = TextArea(style="class:output-field",text=help_text,scrollbar=True)

calculator.py with scrollbar

但是您会添加或更改什么来使用向上翻页和向下翻页键滚动 TextArea?

# The key bindings.
kb = KeyBindings()

@kb.add("pageup")
def _(event):
    # What goes here?
    pass

@kb.add("pagedown")
def _(event):
    # What goes here?
    pass

解决方法

改变焦点

最简单的方法可能是导入 focus_next(或 focus_previous

from prompt_toolkit.key_binding.bindings.focus import focus_next

并将其绑定到 Control-Space(或其他任何东西)。

# The key bindings.
kb = KeyBindings()

kb.add("c-space")(focus_next)

保持专注

您也可以将input_field 的重点放在scroll_page_up 上,导入scroll_page_downfrom prompt_toolkit.key_binding.bindings.page_navigation import scroll_page_up,scroll_page_down

output_field

然后将焦点切换到 scroll_page_up,调用 scroll_page_down/input_field,最后将焦点切换回 # The key bindings. kb = KeyBindings() @kb.add("pageup") def _(event): w = event.app.layout.current_window event.app.layout.focus(output_field.window) scroll_page_up(event) event.app.layout.focus(w) @kb.add("pagedown") def _(event): w = event.app.layout.current_window event.app.layout.focus(output_field.window) scroll_page_down(event) event.app.layout.focus(w)

<asp:Button ID="Button1" class="btn btn-outline-info btn-block btn-sm" runat="server"
 Text="Add to Cart" OnClick="Button1_Click" 
 MyPKID = '<%# Eval("ID") %>'
 MyRow  = '<%# Container.DataItemIndex %>'
 />