Dynamically resize image when frame is maximized

Hello,
I want two images placed side-by-side to increase in size when the frame is resize (maximized). How do I achieve that?

···

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()

Umar Yusuf wrote:

I want two images placed side-by-side to increase in size when the
frame is resize (maximized). How do I achieve that?

A wx.StaticBitmap does not automatically size its contents to fit the
container. You will have to catch the EVT_SIZE event and do the scaling
yourself. Here is a web page with a sample image viewer that shows how
to do the scaling:

···

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

Hi Tim,

Can you help me with a minimal solution for my case.

The link contains too much for me to digest.

Thanks for your time in advance.

···

On Friday, September 22, 2017, Tim Roberts timr@probo.com wrote:

Umar Yusuf wrote:

I want two images placed side-by-side to increase in size when the

frame is resize (maximized). How do I achieve that?

A wx.StaticBitmap does not automatically size its contents to fit the

container. You will have to catch the EVT_SIZE event and do the scaling

yourself. Here is a web page with a sample image viewer that shows how

to do the scaling:

https://www.blog.pythonlibrary.org/2010/03/26/creating-a-simple-photo-viewer-with-wxpython/

Tim Roberts, timr@probo.com

Providenza & Boekelheide, Inc.

You received this message because you are subscribed to the Google Groups “wxPython-users” group.

To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.


*Wouldn’t you rather do Business with us? *
This message is for the designated recipient only and may contain
privileged, proprietary, or otherwise private information. If you have
received it in error, please notify the sender immediately and delete the
original. Any use of the email by you is prohibited. If you have received
this communication in error, please notify the author by replying to this
e-mail immediately.


www.BintaSMS.com <http://www.bintasms.com/>
www.BintaComputers.net <http://www.bintacomputers.net/>

Umar,

Basically when you first draw your images you have to calculate the
space to put them in and scale them to fit and then draw them, (which
you must be doing anyway).

The wx.EVT_SIZE event tells you that the window has changed so you have
to redo the above. You may wish to use some logic to see if the resizing
has finished before doing so as to avoid slowing things down.

···

On 23/09/2017 05:55, Umar Yusuf wrote:

Hi Tim,
Can you help me with a minimal solution for my case.

The link contains too much for me to digest.

Thanks for your time in advance.

--
Steve (Gadget) Barnes
Any opinions in this message are my personal opinions and do not reflect
those of my employer.

---
This email has been checked for viruses by AVG.

Am still struggling with this, my latest effort is: https://pastebin.com/JN6qwbQr

···

On Sat, Sep 23, 2017 at 7:56 AM, Steve Barnes gadgetsteve@live.co.uk wrote:

On 23/09/2017 05:55, Umar Yusuf wrote:

Hi Tim,

Can you help me with a minimal solution for my case.

The link contains too much for me to digest.

Thanks for your time in advance.

Umar,

Basically when you first draw your images you have to calculate the

space to put them in and scale them to fit and then draw them, (which

you must be doing anyway).

The wx.EVT_SIZE event tells you that the window has changed so you have

to redo the above. You may wish to use some logic to see if the resizing

has finished before doing so as to avoid slowing things down.

Steve (Gadget) Barnes

Any opinions in this message are my personal opinions and do not reflect

those of my employer.


This email has been checked for viruses by AVG.

http://www.avg.com

You received this message because you are subscribed to the Google Groups “wxPython-users” group.

To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.


*Wouldn’t you rather do Business with us? *
This message is for the designated recipient only and may contain
privileged, proprietary, or otherwise private information. If you have
received it in error, please notify the sender immediately and delete the
original. Any use of the email by you is prohibited. If you have received
this communication in error, please notify the author by replying to this
e-mail immediately.


www.BintaSMS.com <http://www.bintasms.com/>
www.BintaComputers.net <http://www.bintacomputers.net/>

Well, you’re not really “struggling”, because you’ve only written a few lines of code. I hope you’re not expecting us to write all of this for you; we’ve given you text hint and a link to a sample It’s up to you to do the research.

Here are some points to remember. You are not seeing the difference between the bitmap control and the image that the control holds. When you say self.m_bitmap3.GetSize(), you are getting the size of the control’s window, not the size of the image.

When you get the EVT_SIZE call, you are being told that the control’s window changed size. When you get that notice, YOU need to resize the bitmap, and then send that resized bitmap to the control. That means you’ll need to keep a copy of the original image in your data so you can resize it.

···

On Sep 23, 2017, at 4:57 AM, Umar Yusuf bintacomputers@gmail.com wrote:

Am still struggling with this, my latest effort is: https://pastebin.com/JN6qwbQr


Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

Thanks all for your contributions.
Just for future reference, if someone with similar query found this thread… Here is the solution provided by Rolf of Saxony:-

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()
···

On Sun, Sep 24, 2017 at 6:27 AM, Tim Roberts timr@probo.com wrote:

On Sep 23, 2017, at 4:57 AM, Umar Yusuf bintacomputers@gmail.com wrote:

Am still struggling with this, my latest effort is: https://pastebin.com/JN6qwbQr

Well, you’re not really “struggling”, because you’ve only written a few lines of code. I hope you’re not expecting us to write all of this for you; we’ve given you text hint and a link to a sample It’s up to you to do the research.

Here are some points to remember. You are not seeing the difference between the bitmap control and the image that the control holds. When you say self.m_bitmap3.GetSize(), you are getting the size of the control’s window, not the size of the image.

When you get the EVT_SIZE call, you are being told that the control’s window changed size. When you get that notice, YOU need to resize the bitmap, and then send that resized bitmap to the control. That means you’ll need to keep a copy of the original image in your data so you can resize it.


Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

You received this message because you are subscribed to the Google Groups “wxPython-users” group.

To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.


*Wouldn’t you rather do Business with us? *
This message is for the designated recipient only and may contain
privileged, proprietary, or otherwise private information. If you have
received it in error, please notify the sender immediately and delete the
original. Any use of the email by you is prohibited. If you have received
this communication in error, please notify the author by replying to this
e-mail immediately.


www.BintaSMS.com <http://www.bintasms.com/>
www.BintaComputers.net <http://www.bintacomputers.net/>

Here’s another example:

···

https://github.com/PythonCHB/wxPythonDemos/blob/master/AutoSizeBitmap.py

-CHB

Sent from my iPhone

On Sep 24, 2017, at 5:35 AM, Umar Yusuf bintacomputers@gmail.com wrote:

Thanks all for your contributions.
Just for future reference, if someone with similar query found this thread… Here is the solution provided by Rolf of Saxony:-

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()

On Sun, Sep 24, 2017 at 6:27 AM, Tim Roberts timr@probo.com wrote:

On Sep 23, 2017, at 4:57 AM, Umar Yusuf bintacomputers@gmail.com wrote:

Am still struggling with this, my latest effort is: https://pastebin.com/JN6qwbQr

Well, you’re not really “struggling”, because you’ve only written a few lines of code. I hope you’re not expecting us to write all of this for you; we’ve given you text hint and a link to a sample It’s up to you to do the research.

Here are some points to remember. You are not seeing the difference between the bitmap control and the image that the control holds. When you say self.m_bitmap3.GetSize(), you are getting the size of the control’s window, not the size of the image.

When you get the EVT_SIZE call, you are being told that the control’s window changed size. When you get that notice, YOU need to resize the bitmap, and then send that resized bitmap to the control. That means you’ll need to keep a copy of the original image in your data so you can resize it.


Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

You received this message because you are subscribed to the Google Groups “wxPython-users” group.

To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.


*Wouldn’t you rather do Business with us? *
This message is for the designated recipient only and may contain
privileged, proprietary, or otherwise private information. If you have
received it in error, please notify the sender immediately and delete the
original. Any use of the email by you is prohibited. If you have received
this communication in error, please notify the author by replying to this
e-mail immediately.


www.BintaSMS.com <http://www.bintasms.com/>
www.BintaComputers.net <http://www.bintacomputers.net/>

You received this message because you are subscribed to the Google Groups “wxPython-users” group.

To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.