Hello Folks,
I want to Shrink and expand the Navigation template keeping a button at constant position (Button designed using drawing tools). The position of the button should not change but the template should shrink and expand. Can someone suggest what is wrong here, I m a newbie here. Button highlighted in yellow.
Whoaaa! Nested callbacks !
Let's start with a disclaimer: I've never used vision. So don't take everything that follows as hard truth.
- post the code, with the code formatting tool, not screenshot. Images of code make everything harder than it should for the people trying to help you.
- Don't call functions
func1
or func2
. Give your functions meaningful names.
- Actually I think you switched them. You're calling
func1
, which seems to expand things, when isExpanded
is True. That's why you give functions meaningful names
- The height changes are confusing.
- first, I don't think you should use heights to determine if something is expanded. It should probably be the other way around.
- why are you checking for
height == 600
in func1, when the height has just been set by gui.transform
? same thing for func2
- Why is it 600, when you're setting the height to 400 ?
- is
isExpanded
a custom property ? Are you sure you need to set it yourself, in a callback ?
Here's a rewrite of the code that does the same thing, but is easier to read and process.
Actually, I switched the 2 callbacks
def expand()
if event.source.parent.parent.height == 600:
event.source.isExpanded = True
def shrink()
if event.source.parent.parent.height == 50:
event.source.isExpanded = False
if event.source.isExpanded:
new_height = 50
new_width = 50
callback = shrink
else:
new_height = 400
new_width = 200
callback = expand
system.gui.transform(
event.source.parent.parent,
newHeight=new_height,
duration=250,
callback=lambda: system.gui.transform(
event.source.parent.parent,
newWidth=new_width,
duration=250,
callback=callback
)
)
And here's a version that might also do what you want to do:
if event.source.isExpanded:
new_height = 50
new_width = 50
else:
new_height = 400
new_width = 200
system.gui.transform(
event.source.parent.parent,
newHeight=new_height,
duration=250,
callback=lambda: system.gui.transform(
event.source.parent.parent,
newWidth=new_width,
duration=250,
callback=lambda: event.source.isExpanded = not event.source.isExpanded
)
)
Oh, and one more thing... You haven't stated in your post what your problem actually is.
5 Likes
Thank you for your response and apologies from my side I didn't mention the problem statement.
So basically, what is happening, I can expand the navigation template but can't contract it. At first click it does expand, but once it is expanded it doesn't shrink back.
This thing is happening with code I posted, and same thing is happening with the code you provided.
In your Second Code, getting an Error ["mismatched input '=' expecting RPAREN", ('', 16, 43, '\t\tcallback=lambda: event.source.isExpanded = not event.source.isExpanded\n'))"]
- Yes, "isExpanded" is a custom property.
- I tried switching func1 with func2 in my code, but there is no change
replace that = not
in the last line with is not
You can't assign with is.
But my bad, you can't do that in a lambda. You'd need to define a function and pass it instead.
basically, just replace the expand
and shrink
function with this one:
def resize():
event.source.isExpanded = not event.source.isExpanded
1 Like
I really appreciate your help. Yes. that removed the error. But again, with these codes also its either template either getting Expand or Shrink. It should work vice versa.
Then it's time to start debugging to find why it doesn't work.
I suggest starting with checking what branch your script takes when called. Basically, add a print/log in both if
and else
statements and see which one prints when you're trying to shrink and expand.
If both are the same, then you'll know that isExpanded
stays the same, and you'll have to find why it's not being updated.
If it's different and corresponds to the branch it should be taking, then the problem comes from somewhere else, maybe the gui.transform
calls ?
Proceed step by step, making sure everything along the way works the way it should.
Yes, will have to look into. Thank you for your help.