qtpyqtwindows-10pyqt5enaml

Why won't my Enaml ImageView scale down?


Here is a simple Enaml file to display a single image - an 800x1210 pixel image.

from enaml.widgets.api import Window, Container, ImageView
from enaml.image import Image

enamldef ImageViewResizeWindow(Window):
    Container:
        ImageView:
            image << Image(data=open("Mona_Lisa.jpg", "rb").read())
            scale_to_fit = True
            allow_upscaling = False
            preserve_aspect_ratio = True

The windows opens up rather large to fit the whole image in, but when I try to resize the window, it won't shrink - the image can't be downscaled, even though scale_to_fit is True. On the other hand, it will allow the window to be resized larger, but (correctly) won't upscale the image - it just adds more space around it.

Experimenting, I set allow_upscalingto True, and it allowed the image to grow, but not to shrink. It knows how to scale, but won't downscale.

Okay, maybe the ImageView must have a minimum size, so I added:

            minimum_size = (100, 100)

That should override the "intelligent minimum size" that was calculated for the ImageView widget, but makes no difference.

I am new to Enaml, so I suspect a basic level misunderstanding.

What do I need to do to get an ImageView to allow the downscaling of a large image, when the surrounding window is resized?

Versions:


Solution

  • Setting resist_width and resist_height to 'weak' is the correct answer. You can set other constraints on the widget to give it a preferred size:

    ImageView: 
        resist_width = 'weak'
        resist_height = 'weak'
        constraints = [(width == 640) | 'weak', (height == 480) | 'weak']