wxPython - 在帧最大化时动态调整图像大小

时间:2017-09-22 11:23:29

标签: image python-2.7 wxpython wxpython-phoenix

当帧调整大小(最大化)时,我想要并排放置两个图像以增加大小。我如何实现这一目标?

import wx

class MyFrame2 ( wx.Frame ):

    def __init__( self, parent ):
        wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 500,300 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )

        self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )

        bSizer11 = wx.BoxSizer( wx.HORIZONTAL )

        self.m_bitmap3 = wx.StaticBitmap( self, wx.ID_ANY, wx.Bitmap( u"img/im1.jpg", wx.BITMAP_TYPE_ANY ), wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer11.Add( self.m_bitmap3, 1, wx.ALL|wx.EXPAND, 5 )

        self.m_bitmap4 = wx.StaticBitmap( self, wx.ID_ANY, wx.Bitmap( u"img/im2.jpg", wx.BITMAP_TYPE_ANY ), wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer11.Add( self.m_bitmap4, 1, wx.ALL|wx.EXPAND, 5 )


        self.SetSizer( bSizer11 )
        self.Layout()

        self.Centre( wx.BOTH )

    def __del__( self ):
        pass

app = wx.App(0)
MyFrame2(None).Show()
app.MainLoop()

2 个答案:

答案 0 :(得分:1)

基于您的pastebin代码

import wx

class MyFrame2 ( wx.Frame ):

    def __init__( self, parent ):
        wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 500,300 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
        self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
        bSizer11 = wx.BoxSizer( wx.HORIZONTAL )
        self.img1=wx.Image("1.bmp", wx.BITMAP_TYPE_ANY)
        self.img2=wx.Image("1.bmp", wx.BITMAP_TYPE_ANY)
        self.m_bitmap3 = wx.StaticBitmap( self, wx.ID_ANY, wx.BitmapFromImage(self.img1), wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer11.Add( self.m_bitmap3, 1, wx.EXPAND, 0 )
        self.m_bitmap4 = wx.StaticBitmap( self, wx.ID_ANY, wx.BitmapFromImage(self.img2))
        bSizer11.Add( self.m_bitmap4, 1, wx.EXPAND, 0 )
        self.Bind(wx.EVT_SIZE, self.onResize)
        self.SetSizer( bSizer11 )
        self.Layout()
        self.Centre(wx.BOTH)

    def __del__( self ):
        pass

    def onResize(self, event):
        # self.Layout()
        frame_size = self.GetSize()
        frame_h = (frame_size[0]-10) / 2
        frame_w = (frame_size[1]-10) / 2
        img1 = self.img1.Scale(frame_h,frame_w)
        img2 = self.img2.Scale(frame_h,frame_w)
        self.m_bitmap3.SetBitmap(wx.BitmapFromImage(img1))
        self.m_bitmap4.SetBitmap(wx.BitmapFromImage(img2))
        self.Refresh()
        self.Layout()

app = wx.App(0)
MyFrame2(None).Show()
app.MainLoop()

注意:单self.BindScale的大小基于帧大小的一半,每次从原始图像缩放图像,否则它们会慢慢变得越来越扭曲。

答案 1 :(得分:0)

您需要绑定到wx.EVT_SIZE,因为这是调整帧大小时触发的事件。然后在该处理程序中,您将需要更新图像的大小。您可以使用Scale()中的wx.Image方法更改图片的大小。请注意,您可能希望以这样的方式缩放图像,使其保持其纵横比,或者在拉伸时看起来很奇怪。

这是一个基于几年前我的tutorial照片查看器的例子:

import os
import wx

class PhotoCtrl(wx.App):
    def __init__(self, redirect=False, filename=None):
        wx.App.__init__(self, redirect, filename)
        self.frame = wx.Frame(None, title='Photo Control', size=(400, 400))
        self.image_loaded = False
        self.current_size = self.frame.GetSize()
        self.filepath = None

        self.panel = wx.Panel(self.frame)
        self.Bind(wx.EVT_SIZE, self.onResize)

        self.PhotoMaxSize = self.current_size.GetHeight() - 10

        self.createWidgets()
        self.frame.Show()

    def createWidgets(self):
        instructions = 'Browse for an image'
        img = wx.EmptyImage(240,240)
        self.imageCtrl = wx.StaticBitmap(self.panel, wx.ID_ANY,
                                         wx.BitmapFromImage(img))

        instructLbl = wx.StaticText(self.panel, label=instructions)
        self.photoTxt = wx.TextCtrl(self.panel, size=(200,-1))
        browseBtn = wx.Button(self.panel, label='Browse')
        browseBtn.Bind(wx.EVT_BUTTON, self.onBrowse)

        self.mainSizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer = wx.BoxSizer(wx.HORIZONTAL)

        self.mainSizer.Add(wx.StaticLine(self.panel, wx.ID_ANY),
                           0, wx.ALL|wx.EXPAND, 5)
        self.mainSizer.Add(instructLbl, 0, wx.ALL, 5)
        self.mainSizer.Add(self.imageCtrl, 0, wx.ALL, 5)
        self.sizer.Add(self.photoTxt, 0, wx.ALL, 5)
        self.sizer.Add(browseBtn, 0, wx.ALL, 5)
        self.mainSizer.Add(self.sizer, 0, wx.ALL, 5)

        self.panel.SetSizer(self.mainSizer)

        self.panel.Layout()

    def onBrowse(self, event):
        """
        Browse for file
        """
        wildcard = "JPG files (*.jpg)|*.jpg"
        dialog = wx.FileDialog(None, "Choose a file",
                               wildcard=wildcard,
                               style=wx.OPEN)
        if dialog.ShowModal() == wx.ID_OK:
            self.photoTxt.SetValue(dialog.GetPath())
            self.onView()
        dialog.Destroy()

    def scale_image(self):
        if self.filepath:
            img = wx.Image(self.filepath, wx.BITMAP_TYPE_ANY)
            # scale the image, preserving the aspect ratio
            W = img.GetWidth()
            H = img.GetHeight()
            if W > H:
                NewW = self.PhotoMaxSize
                NewH = self.PhotoMaxSize * H / W
            else:
                NewH = self.PhotoMaxSize
                NewW = self.PhotoMaxSize * W / H
            img = img.Scale(NewW,NewH)
            return img


    def onView(self):
        self.filepath = self.photoTxt.GetValue()
        img = self.scale_image()
        self.imageCtrl.SetBitmap(wx.BitmapFromImage(img))
        self.panel.Refresh()
        self.image_loaded = True

    def onResize(self, event):
        print 'resizing'
        if self.image_loaded:
            if self.current_size != self.frame.GetSize():
                self.current_size = self.frame.GetSize()

                self.PhotoMaxSize = self.current_size.GetHeight() - 30
                img = self.scale_image()
                self.imageCtrl.SetBitmap(wx.BitmapFromImage(img))
                self.panel.Refresh()
                self.panel.Layout()


if __name__ == '__main__':
    app = PhotoCtrl()
    app.MainLoop()

请注意,这似乎可以很好地缩放图像,但是底部的按钮会被切掉。我不确定为什么并且现在没有时间来诊断这个问题,但总的来说这可能就是你想要的方式。