Cannot focus DynamicContainer
See original GitHub issueI would like to have the contents of a container update based on what is focused. I figured that using a DynamicContainer
was the best way to achieve this. However, whenever I put a normally focusable widget in a DynamicContainer
, it seems to be impossible to focus it. Here’s an example:
bindings = KeyBindings()
bindings.add("c-c")(lambda event: event.app.exit())
bindings.add("tab")(focus_next)
bindings.add("s-tab")(focus_previous)
regular_button = Button("Regular")
dynamic_button = DynamicContainer(lambda: Button("Dynamic"))
layout = Layout(
HSplit([
regular_button,
dynamic_button,
]),
)
app = Application(layout=layout, key_bindings=bindings)
app.run()
In this example, I can focus regular_button
but not dynamic_button
. If I try to focus it using layout.focus(dynamic_button)
, I get the following stack trace:
Traceback (most recent call last):
File "/home/garrett/.PyCharm2018.2/config/scratches/scratch_39.py", line 24, in <module>
layout.focus(dynamic_button)
File "/home/garrett/.local/share/virtualenvs/skiddie-x-macEj3/lib/python3.5/site-packages/prompt_toolkit/layout/layout.py", line 141, in focus
raise ValueError('Invalid value. Container cannot be focused: %r' % (value, ))
ValueError: Invalid value. Container cannot be focused: <prompt_toolkit.layout.containers.DynamicContainer object at 0x7f725252e780>
If this is intended behavior, I would really appreciate it if anyone could let me know of another way to achieve what I’m trying to do.
Thanks, Garrett
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Buttons in DynamicContainer don't seem to receive focus #1324
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...
Read more >Solved: queue a dynamic container type - NI Community
Solved: Hi, I have several threads running, each waiting for an input queue (queue names are numbered by threads: "Queue0",
Read more >Reference — prompt_toolkit 3.0.36 documentation
Start a background task (coroutine) for the running application. When the Application terminates, unfinished background tasks will be cancelled. Given that we ...
Read more >Focus Reset and Guided Focus Management
Focus reset happens when an active (currently focused) element, gets removed form the DOM or the render tree. On focus reset, document.
Read more >Adding a dynamic container to a portal - Pega
Dynamic containers display content in the main pane of the portal. You can define a default view for a container, but if the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi @lostatc,
I noticed the problem is on the following line:
This creates every a different button every single time. That doesn’t work because
focus_next
first tries to enumerate all widgets that are focusable, then checks in that list what the current widget is, and goes to the next one. By that time, this button is already gone. If we would set this specific instance as the focused button, the renderer also wouldn’t know where to find it.Try to rewrite it as follows:
Maybe, that’s some thing that should be mentioned in the documentation of
DynamicContainer
.Thanks for all the help @jonathanslenders! That answers my question. I’ll go ahead and close this issue.