简体   繁体   English

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

[英]How do you add pageup/pagedown keybindings to TextArea in python-prompt-toolkit?

Let's use calculator.py for example.让我们以calculator.py为例。

To add a scrollbar that works with the mouse wheel, you would change:要添加与鼠标滚轮配合使用的滚动条,您需要更改:

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

没有滚动条的calculator.py

to:至:

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

带有滚动条的calculator.py

But what would you add or change to scroll the TextArea with the page up and page down keys?但是,您会添加或更改什么以使用向上翻页和向下翻页键滚动 TextArea?

# The key bindings.
kb = KeyBindings()

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

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

Change focus改变焦点

The simplest way would probably be to import focus_next (or focus_previous )最简单的方法可能是导入focus_next (或focus_previous

from prompt_toolkit.key_binding.bindings.focus import focus_next

and bind it to Control-Space (or anything else).并将其绑定到 Control-Space(或其他任何东西)。

# The key bindings.
kb = KeyBindings()

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

Keep focus保持专注

You could also, to seemingly keep the focus on input_field , import scroll_page_up and scroll_page_down您也可以,为了看起来将注意力集中在input_field上,导入scroll_page_upscroll_page_down

from prompt_toolkit.key_binding.bindings.page_navigation import scroll_page_up, scroll_page_down

then switch focus to output_field , call scroll_page_up / scroll_page_down and finally change focus back to input_field .然后将焦点切换到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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM