簡體   English   中英

如何在 python-prompt-toolkit 中將 pageup/pagedown 鍵綁定添加到 TextArea?

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

讓我們以calculator.py為例。

要添加與鼠標滾輪配合使用的滾動條,您需要更改:

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

沒有滾動條的calculator.py

至:

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

帶有滾動條的calculator.py

但是,您會添加或更改什么以使用向上翻頁和向下翻頁鍵滾動 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_upscroll_page_down

from 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM