prompt-toolkit / prompt-toolkit/python-prompt-toolkit
Buttons in DynamicContainer don't seem to receive focus
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 10.6k
- Forks
- 815
- PR merge metrics
- No merged PRs in 30d
Description
I've been trying to use some buttons inside a DynamicContainer, but I can't seem to get the buttons to have focus properly so that I can select them.
My code is below:
from prompt_toolkit import Application
from prompt_toolkit.application.current import get_app
from prompt_toolkit.key_binding.bindings.focus import focus_next, focus_previous
from prompt_toolkit.key_binding.key_bindings import KeyBindings
from prompt_toolkit.layout import HSplit, Layout
from prompt_toolkit.layout.containers import DynamicContainer, FloatContainer
from prompt_toolkit.styles import Style
from prompt_toolkit.widgets import Button
def get_container_contents():
return HSplit([Button(text="Click here", handler=lambda: print("Hello")), Button("Click here instead")])
def create_prompt_toolkit_app():
kb = KeyBindings()
kb.add("tab")(focus_next)
kb.add("s-tab")(focus_previous)
@kb.add("escape")
def exit_handler(event=None) -> None:
"Button handler that exits the app"
get_app().exit()
style = Style(
[
("button", "#00ff00"),
("button.focused", "fg:#ff0000"),
]
)
root_container = DynamicContainer(get_container_contents)
#root_container = FloatContainer(get_container_contents(), floats=[])
layout = Layout(root_container)
app = Application(layout=layout, full_screen=True, key_bindings=kb, style=style)
return app
app = create_prompt_toolkit_app()
app.run()
You can see there are two lines of code defining the root_container, and one is commented out. If I use the DynamicContainer one of those lines, then I get an app where I can't move the focus between the two buttons. If I press Enter in this app, then hello gets printed, so it seems like the button sort-of has focus (in that it responds to an Enter keypress), but it isn't shown as having focus (according to the style), and I can't seem to move the focus.
If, I replace that line of code with the FloatContainer one of those lines, then everything works completely normally: I can move the focus around, and pressing Enter on the different buttons behaves as expected.
I've tried setting the focus manually using app.layout.focus() but that doesn't seem to change anything.
Does anyone have any idea what is going on here? I'm sure I've used DynamicContainers ok before without this problem.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Use the supplied Python reproduction with DynamicContainer, FloatContainer, Layout, and focus_next/focus_previous. Compare how each container participates in focus traversal and verify that focus moves between both buttons and their focused style appears; the reproduction should behave consistently with FloatContainer.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- cli
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100