Constraints questions about spacer

33 views
Skip to first unread message

Brian Parma

unread,
Mar 18, 2019, 9:46:26 PM3/18/19
to Enaml

I'm having some trouble getting the result I want from spacer.  I am trying to replicate something created in Qt Desginer.  It's essentially a grid container that takes up a fixed width, and to center that container in the window a horizontal spacer is placed on the left and right side of it.

In designer, both the spacers have equal "weight", so they take up the same amount of space, but I can't figure out how to do this in enaml.  If I just try:

```
hbox(spacer, grid(...), spacer)
```

Then the first spacer takes up all the space.  Is there a way to tell them to take up equal space (equal weights)?  Since they are created in-place, I don't have ids to add an additional constraint with.  Or is there a way to say "center grid"?


Matthieu Dartiailh

unread,
Mar 20, 2019, 7:14:24 PM3/20/19
to Brian Parma, Enaml
Hi Brian,

Spacer themselves are not constrainable so you cannot impose the equality constraint directly on the spacer. One way around this is to define constraints with respect to the container edges and some elements in the grid. I illustrates this possibility below.

Another option would be to wrap the grid into a container but I guess that you are trying to avoid that.

Best

Matthieu

from enaml.layout.api import grid, hbox, spacer
from enaml.widgets.api import Window, Container, Label, PushButton


enamldef Main(Window):
Container:
constraints = [
hbox(spacer,
grid((lbl_a, lbl_b, lbl_c),
(lbl_d, lbl_e, lbl_f),
(lbl_g, lbl_g, lbl_g),
(btn_1, btn_2, btn_3)),
spacer),
lbl_a.left - contents_left == contents_right - lbl_c.right
]
Label: lbl_a:
text = "Label A"
Label: lbl_b:
text = "Label B"
Label: lbl_c:
text = "Label C"
Label: lbl_d:
text = "Label D"
Label: lbl_e:
text = "Label E"
Label: lbl_f:
text = "Label F"
Label: lbl_g:
text = "Label G"
PushButton: btn_1:
text = "Button 1"
PushButton: btn_2:
text = "Button 2"
PushButton: btn_3:
text = "Button 3"

--
You received this message because you are subscribed to the Google Groups "Enaml" group.
To unsubscribe from this group and stop receiving emails from it, send an email to enaml+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Matthieu Dartiailh

unread,
Mar 20, 2019, 8:04:40 PM3/20/19
to Brian Parma, Enaml
Actually you can also do that. I forgot that the helpers are actually also constrainable.

from enaml.layout.api import grid, hbox, spacer, align
from enaml.widgets.api import Window, Container, Label, PushButton


enamldef Main(Window):
Container:
layout_constraints => ():
g = grid((lbl_a, lbl_b, lbl_c),
(lbl_d, lbl_e, lbl_f),
(lbl_g, lbl_g, lbl_g),
(btn_1, btn_2, btn_3))
constraints = [hbox(spacer,g, spacer) ,
align('h_center', g, contents_h_center)
]
return constraints
Label: lbl_a:
text = "Label A"
Label: lbl_b:
text = "Label B"
Label: lbl_c:
text = "Label C"
Label: lbl_d:
text = "Label D"
Label: lbl_e:
text = "Label E"
Label: lbl_f:
text = "Label F"
Label: lbl_g:
text = "Label G"
PushButton: btn_1:
text = "Button 1"
PushButton: btn_2:
text = "Button 2"
PushButton: btn_3:
text = "Button 3"


Brian Parma

unread,
Mar 20, 2019, 8:25:49 PM3/20/19
to Matthieu Dartiailh, Enaml
Ah thanks, didn't know about `content_h_center`.
Reply all
Reply to author
Forward
0 new messages