Display a bitmap

Given

panel = wx.Panel(self,-1)

bmp = wx.Image(imageFile, wx.BITMAP_TYPE_BMP).ConvertToBitmap()

Now Display the bmp on the panel

without clicking, resizing or shrinking the window

···

…but how? ??

#-----------------------------------------------------------

TIA
Bob

srfpala wrote:

Given
panel = wx.Panel(self,-1)
bmp = wx.Image(imageFile, wx.BITMAP_TYPE_BMP).ConvertToBitmap()

        \#  Now Display the bmp on the panel
        \#  without clicking, resizing or shrinking the window
        \#
        \#  \.\.\.\.but  how? ??

The easy way is to add a wx.StaticBitmap control to fill the panel, and call SetBitmap to assign the bitmap to that control. It will do everything necessary to display the image.

If you really, really want to do it yourself, you'll need to catch the EVT_PAINT message and use wx.DC.DrawBitmap.

···

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

I’m trying to figure out why the code below isn’t working.

if dlg.ShowModal() == wx.ID_OK:

path = dlg.GetPath()

imageFile = path

print("imageFile = ",imageFile)

panel = wx.Panel(self,-1)

bmpSample = wx.Image(imageFile, wx.BITMAP_TYPE_BMP).ConvertToBitmap()

self.button2 = wx.BitmapButton(panel, -1, bmpSample,

pos=(10, 10), size=(600,400), style=0)
# =========================================

I thought the above ought to display the image

however one must either resize the window to see it or

shrink it to the taskbar then recall it to see the image.

Both activate a windows intervention requiring a repaint… I think

        # Can I force a repaint within python code ?
        # Strange - any ideas ? app
···

panel.repaint() did nothing

-------------------------------------- End onOFD

You created a new panel, and did not give it a size, so it probably defaulted to a small size like (20,20). It doesn’t matter that the bitmap button is sized to (600,400), if its parent is not at least that big then it will be clipped to the size of the parent. When you resize the window it triggers the default EVT_SIZE handlers, which in the case of wx.Frame (which is what I assume self is above) will check if it has only one child (like the panel) and will then resize and reposition it to fill the client area of the frame.

A better approach would be to create all the widgets (the frame, panel, static bitmap, and whatever else you need, like maybe a button or menu to trigger opening the file dialog, etc.) at the start, as part of the frame’s init method. Then later when you get the desired image filename from the user all you need to do is create the bitmap and do something like

self.static_bmp.SetBitmap(theBitmap)
···

On Wednesday, June 5, 2019 at 4:17:35 PM UTC-7, srfpala wrote:

I’m trying to figure out why the code below isn’t working.

if dlg.ShowModal() == wx.ID_OK:

path = dlg.GetPath()

imageFile = path

print("imageFile = ",imageFile)

panel = wx.Panel(self,-1)

bmpSample = wx.Image(imageFile, wx.BITMAP_TYPE_BMP).ConvertToBitmap()

self.button2 = wx.BitmapButton(panel, -1, bmpSample,

pos=(10, 10), size=(600,400), style=0)
# =========================================

I thought the above ought to display the image

however one must either resize the window to see it or

shrink it to the taskbar then recall it to see the image.

Both activate a windows intervention requiring a repaint… I think

        # Can I force a repaint within python code ?
        # Strange - any ideas ? app

Robin

Well, I would like to remove the button and place the image on the panel.
path = dlg.GetPath()

imageFile = path

panel = wx.Panel(self,-1,size=(600,400))

bmpSample = wx.Image(imageFile, wx.BITMAP_TYPE_BMP).ConvertToBitmap()

Instantiate a button and place the image on it.

self.button2 = wx.BitmapButton(panel, -1, bmpSample,

pos=(10, 10), size=(600,400), style=0)

the above works fine

···

#---------------------------------------------------

instead, let’s just display the image on the panel as follows

Mybitmap = wx.Bitmap(imageFile, type=wx.BITMAP_TYPE_BMP)

self.bitmap = wx.StaticBitmap(self.panel, bitmap=bmpSample)

App loads menu items image file is selected but

        # the image is not displayed !

Any ideas ?

TIA
Bob

BTW
Tried
self.static_bmp.SetBitmap(bmpSample)

Unfortunately no image is displayed !

Any ideas ?

···

On Tuesday, June 4, 2019 at 5:34:44 PM UTC-5, srfpala wrote:

Given

panel = wx.Panel(self,-1)

bmp = wx.Image(imageFile, wx.BITMAP_TYPE_BMP).ConvertToBitmap()

Now Display the bmp on the panel

without clicking, resizing or shrinking the window

…but how? ??

#-----------------------------------------------------------

TIA
Bob

You aren't using any sizers, so your StaticBitmap is not going to resize itself automatically. Create a wx.BoxSizer(wx.HORIZONTAL), attach it to the panel, and add the StaticBitmap to it.

···

On Jun 9, 2019, at 2:29 PM, srfpala <srfpala@gmail.com> wrote:

Well, I would like to remove the button and place the image on the panel.


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