Open robintw opened 3 years ago
I've looked into this some more, and if I change the Button
instances to be stored in global variables, rather than defined inline in the function, then it works. The top of the new code looks like this:
button1 = Button(text="Click here", handler=lambda: print("Hello"))
button2 = Button("Click here instead")
def get_container_contents():
return Box(body=HSplit([button1, button2]))
I'm not sure what's going on with this, and why it only works if the variables are in a different scope. I've found similar things in my actual code (this is a simplified example for posting here), where if I store the Button
object as a member variable of the class, and create it in the __init__
, then it works fine, but if I have it as a member variable but create it in the get_container_contents
function then it doesn't.
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:
You can see there are two lines of code defining the
root_container
, and one is commented out. If I use theDynamicContainer
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, thenhello
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.