wxpython:自动调整静态图像(staticbitmap)的大小以适应大小 [英] wxpython: automatically resize a static image (staticbitmap) to fit into size

查看:97
本文介绍了wxpython:自动调整静态图像(staticbitmap)的大小以适应大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 wxPython 项目有一个框架,带有多个嵌套的 sizer.

My wxPython project has a frame, with multiple nested sizers.

其中一个 sizer 包含一个 wxStaticImage 和一个从文件中读取的位图.

One of the sizers contains a wxStaticImage with a bitmap that is read from a file.

每次调整框架大小时,我都需要图像调整大小(增大/缩小),以便它适合它的 sizer 边界.

I need the image to resize (grow/shrink) every time the frame is resized, so it will fit it's sizer's boundaries.

(我认为)我知道如何调整图像大小.我不知道的是如何:

(I think that) I know how to resize an image. What I don't know is how to:

  • 如何获取图像容器的宽度或高度?
  • 也许我忽略了一个自动执行的属性?

(目前,我不介意比例)

(for now, I don't mind the proportions)

完整的解决方案

  1. 我对 wxStaticBitmapin.Size 的理解有误.它不描述 图像 的大小(即图像分辨率),而是 - wxStaticBitmapin.Size 给出了 sizer 的插槽尺寸,或者换句话说:当前小部件的大小.

  1. i understood wrong about wxStaticBitmapin.Size. it does NOT describe the size of the image (i.e. image resolution), but rather - wxStaticBitmapin.Size gives the sizer's slot dimentions, or in other words: the current widget's size.

所以有了 Mik 的代码,我现在如何适应插槽.

so with Mik's code i now how to fit into the slot.

除了 mike 的解决方案:在框架上使用 onSize 事件时,不要忘记添加 event.skip().否则,sizer 将停止重新对齐.或者,只需使用图像的 onSize.

in addition to mike's solution: when using onSize event on a frame, don't forget to add event.skip(). otherwise the sizers will stop re-aligning. Altertanively, just use the image's onSize.

这里是完整的事件方法:

here's the complete event method:

def bitmap1_onSize(self, e=None):
    W, H = self.bitmap1.Size
    if W > H:
        NewW = W
        NewH = W * H / W
    else:
        NewH = H
        NewW = H * W / H
    img = wx.Image(self.frame_file_picker.Path, wx.BITMAP_TYPE_ANY)
    img = img.Scale(NewW,NewH)
    self.bitmap1.SetBitmap(wx.BitmapFromImage(img))
    e.Skip()

推荐答案

您需要捕获 EVT_SIZE 或 EVT_SIZE.您可以查看我写的有关创建图像查看器的教程.它有一些缩放代码:http://www.blog.pythonlibrary.org/2010/03/26/creating-a-simple-photo-viewer-with-wxpython/

You will need to catch EVT_SIZE or EVT_SIZING. You can check out this tutorial I wrote about creating an image viewer. It has some scaling code in it: http://www.blog.pythonlibrary.org/2010/03/26/creating-a-simple-photo-viewer-with-wxpython/

我会使用该缩放代码并使用它来更新您的图像.您需要确保停止将图像放大到超过其最大尺寸,否则最终会出现大量像素化.

I would just take that scaling code and use it to update your image. You'll want to make sure you stop scaling your image up past its maximum size or you'll end up with a lot of pixelization though.

这篇关于wxpython:自动调整静态图像(staticbitmap)的大小以适应大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆