Refreshing an Image in wx.StaticBitmap

Hi,

I've been messing around trying to figure out how to make an application
that lets a user choose an image to preview and I cannot get it to work
properly. Ideally, I would allow the user to enter the path to the image
or select it from a FileDialog. The problem isn't with that, however. The
problem is getting the chosen image to display. I started with
wx.StaticBitmap and it works great, if I make the program load an image
BEFORE the application is fully loaded.

If I try to update it with another image of the user's choosing, either
nothing happens or a little black square will appear behind the already
loaded photo. I tried loading an EmptyImage at startup and that worked
too, but updating doesn't work either. I guess I don't know how to set the
control to display a different image. I see there's a Replace() method,
but I don't understand how to use it.

The only workaround I found that kind of works is making a button call and
display a wx.PopupWindow with the image on that. And yes, I checked the
wiki and googled all over. I try to never post without trying diligently
to find the answer on my own.

Currently using Python 2.4, wxPython 2.8.4.2 on Windows XP. Thanks!

Mike Driscoll
Applications Specialist
MCIS - Technology Center

Hi Mike,

I've been messing around trying to figure out how to make an application
that lets a user choose an image to preview and I cannot get it to work
properly. Ideally, I would allow the user to enter the path to the image
or select it from a FileDialog. The problem isn't with that, however. The
problem is getting the chosen image to display. I started with
wx.StaticBitmap and it works great, if I make the program load an image
BEFORE the application is fully loaded.

If I try to update it with another image of the user's choosing, either
nothing happens or a little black square will appear behind the already
loaded photo. I tried loading an EmptyImage at startup and that worked
too, but updating doesn't work either. I guess I don't know how to set the
control to display a different image. I see there's a Replace() method,
but I don't understand how to use it.

The only workaround I found that kind of works is making a button call and
display a wx.PopupWindow with the image on that. And yes, I checked the
wiki and googled all over. I try to never post without trying diligently
to find the answer on my own.

Currently using Python 2.4, wxPython 2.8.4.2 on Windows XP. Thanks!
  

I found an old sample I made which works fine and could fit your needs with some adaptation. It's coming at the end of this message.
Also, what could be helpful should be to post a small sample of what you are trying to do. Make it as simple as possible, I'm sure it would be possible to eliminate the problem.
Regards,
jm

import wx
class MainWindow(wx.Frame):
    def __init__(self,parent,title):
        self.imgList = ['Contacts.png','Contracts.png','Customers.png']
        self.imgInd = 0
        wx.Frame.__init__(self,parent,wx.ID_ANY,title,style=wx.DEFAULT_FRAME_STYLE|wx.NO_FULL_REPAINT_ON_RESIZE)
        self.sizer1 = wx.BoxSizer(wx.VERTICAL)
        jpg = wx.Image(self.imgList[self.imgInd],wx.BITMAP_TYPE_PNG).ConvertToBitmap()
        self.objBmp = wx.StaticBitmap(self,wx.ID_ANY,jpg,size=(128,128),style=wx.SUNKEN_BORDER)
        self.sizer1.Add(self.objBmp)
        button = wx.Button(self,wx.ID_ANY,'change image')
        self.sizer1.Add(button)
        button.Bind(wx.EVT_BUTTON,self.OnButton)
        self.SetSizer(self.sizer1)
        self.Fit()
        self.Show(1)
    def OnButton(self, event):
        self.imgInd += 1
        if self.imgInd >= len(self.imgList):
            self.imgInd = 0
        jpg = wx.Image(self.imgList[self.imgInd],wx.BITMAP_TYPE_PNG).ConvertToBitmap()
        self.objBmp.Destroy()
        self.objBmp= wx.StaticBitmap(self,wx.ID_ANY,jpg,size=(128,128),style=wx.SUNKEN_BORDER)
app = wx.PySimpleApp()
frame=MainWindow(None,'bmp sample')
app.MainLoop()
del app

Mike Driscoll wrote:

Hi,

I've been messing around trying to figure out how to make an application
that lets a user choose an image to preview and I cannot get it to work
properly. Ideally, I would allow the user to enter the path to the image
or select it from a FileDialog. The problem isn't with that, however. The
problem is getting the chosen image to display. I started with
wx.StaticBitmap and it works great, if I make the program load an image
BEFORE the application is fully loaded.

If I try to update it with another image of the user's choosing, either
nothing happens or a little black square will appear behind the already
loaded photo.

You are probably creating a new wx.StaticBitmap instead of just loading a new wx.Bitmap into the current one. This is a common mistake that newbies make. If that is not what you are doing then pleas make a small runnable sample that shows your problem and share it with the list.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Hi!

From: jean-michel bain-cornu [mailto:pythonnews@nospam.jmbc.fr]
Sent: Tuesday, September 25, 2007 4:12 PM
To: wxPython-users@lists.wxwidgets.org
Subject: Re: [wxPython-users] Refreshing an Image in wx.StaticBitmap

Hi Mike,
> I've been messing around trying to figure out how to make an
> application that lets a user choose an image to preview and
I cannot
> get it to work properly. Ideally, I would allow the user to
enter the
> path to the image or select it from a FileDialog. The problem isn't
> with that, however. The problem is getting the chosen image to
> display. I started with wx.StaticBitmap and it works great,
if I make
> the program load an image BEFORE the application is fully loaded.
>
> If I try to update it with another image of the user's choosing,
> either nothing happens or a little black square will appear
behind the
> already loaded photo. I tried loading an EmptyImage at startup and
> that worked too, but updating doesn't work either. I guess I don't
> know how to set the control to display a different image. I see
> there's a Replace() method, but I don't understand how to use it.
>
> The only workaround I found that kind of works is making a
button call
> and display a wx.PopupWindow with the image on that. And yes, I
> checked the wiki and googled all over. I try to never post without
> trying diligently to find the answer on my own.
>
> Currently using Python 2.4, wxPython 2.8.4.2 on Windows XP. Thanks!
>
I found an old sample I made which works fine and could fit
your needs with some adaptation. It's coming at the end of
this message.
Also, what could be helpful should be to post a small sample
of what you are trying to do. Make it as simple as possible,
I'm sure it would be possible to eliminate the problem.
Regards,
jm

I tried your suggestion, but I must be doing something wrong. I've
attached my original implementation (Example1.py), my implementation in
which I show the photo in a popupdialog widget (Example2.py) and your
suggestion.

Basically what I did was modify my button's handler in Example1.py with
your code and created Example3.py as a result. When I do it though, I end
up with a blank square that looks like a sunken block in my panel.

Thanks for your help and (hopefully) constructive comments.

Mike

example1.py (1.19 KB)

example2.py (3.94 KB)

example3.py (1.24 KB)

···

-----Original Message-----