nucleic / enaml

Declarative User Interfaces for Python
http://enaml.readthedocs.io/en/latest/
Other
1.53k stars 130 forks source link

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

Open Kochise opened 2 years ago

Kochise commented 2 years ago

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

MatthieuDartiailh commented 2 years ago

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]
Kochise commented 2 years ago

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.

MatthieuDartiailh commented 2 years ago

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.

Kochise commented 2 years ago

mplcanvas_resizing

Kochise commented 2 years ago

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