enaml icon indicating copy to clipboard operation
enaml copied to clipboard

[DOC] How to set the size of a Widget ?

Open Kochise opened this issue 2 years ago • 5 comments

I have a MPLCanvas I'd like to reshape at runtime and the whole parent Window to resize accordingly, how is it possible ?

All what've found so far is Window.set_size() but I need the MPLCanvas child to have a specific size instead.

https://enaml.readthedocs.io/en/latest/api_ref/widgets/window.html?highlight=size#enaml.widgets.window.Window.set_size

Kochise avatar Nov 05 '21 09:11 Kochise

This is not the typical way to use enaml layout engine but you can specify the width and height as part of the constraints on the container which is the parent of your canvas as below (sizes are in pixel):

enamldef Main(Window):
    Container:
        constraints = [
            vbox(
                hbox(cbox, check, spacer),
                canvas,
            ),
            cbox.v_center == check.v_center,
            canvas.width == 1024,
            canvas.height == 480,
        ]
        ComboBox: cbox:
            items = ['one', 'two']
            index = 0
        CheckBox: check:
            text = 'Toolbar Visible'
            checked := canvas.toolbar_visible
        MPLCanvas: canvas:
            figure << figures[cbox.selected_item]

MatthieuDartiailh avatar Nov 05 '21 10:11 MatthieuDartiailh

Yet I'd like to do MPLCanvas.set_size() instead, because I only need to set it's size from time to time.

I use the MPLCanvas to display a VNC connection and regarding the server screen's size, update the canvas' size accordingly.

Then I don't want to prevent the user from resizing the parent Window to its liking though.

Btw, thank for the hint.

Kochise avatar Nov 05 '21 10:11 Kochise

Setting the constraints can be done dynamically (be careful to no simply update the list in place though) and will get you what you want. You can access the canvas parent using the parent attribute. You can even have you canvas alone in a container in which case you size constraints will be the only ones.

MatthieuDartiailh avatar Nov 05 '21 10:11 MatthieuDartiailh

mplcanvas_resizing

Kochise avatar Nov 05 '21 10:11 Kochise

The problem is even a Container is derived from Widget which doesn't support .set_size().

When I say I'd like to "resize" the canvas, it's only at the connection, then leave the user "unconstrained" to resize the window.

The best would be to set a "constraint" on the canvas' ratio, so that when changing the Window width, its height also adapts accordingly in real time.

snow_ratio_resize

Kochise avatar Nov 05 '21 10:11 Kochise