Button event to change image help

485 views
Skip to first unread message

JeffMGreg

unread,
Jan 10, 2011, 6:47:45 PM1/10/11
to wxPython-users
I've looked all over for this answer and I can't find it anywhere.
Hope someone can help me with this. Basically, I want to find a way
of changing an image whenever a button is clicked. I don't want to
over write the image because sometimes the sizes will be different and
that looks awful. Any help would be greatly appreciated.

this is a little example of what I would want just with the image
changing every click.

import wx
IMAGE = 0
class Main(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, parent = None, id = -1, title =
'Button testing')

topSizer = wx.BoxSizer(wx.HORIZONTAL)

error = wx.Bitmap('error.png')
checkmark = wx.Bitmap('checkmark.png')
help = wx.Bitmap('help.png')
self.list = [error, checkmark, help]

bmp = wx.StaticBitmap(self, -1, self.list[IMAGE])
button = wx.Button(self, 1, 'Test')

topSizer.Add(button, 0)
topSizer.Add(bmp, 1)
self.SetSizer(topSizer)

self.Bind(wx.EVT_BUTTON, self.buttonclicked)
self.Center()
self.Show(True)

def buttonclicked(self, event):
# remove current image and place next image in self.list
print 'in buttonclicked'
IMAGE += 1
# ... ?

app = wx.App()
Main()
app.MainLoop()

Tim Roberts

unread,
Jan 10, 2011, 8:30:32 PM1/10/11
to wxpytho...@googlegroups.com
JeffMGreg wrote:
> I've looked all over for this answer and I can't find it anywhere.
> Hope someone can help me with this. Basically, I want to find a way
> of changing an image whenever a button is clicked. I don't want to
> over write the image because sometimes the sizes will be different and
> that looks awful. Any help would be greatly appreciated.

What do you want it do to if you pick an image of a different size? In
my image viewer, I just do this:

bmp = wx.Image( sel, mappings[ext] ).ConvertToBitmap()
sz = (bmp.GetWidth(), bmp.GetHeight())
self.img.SetSize( sz )
self.img.SetBitmap( bmp )
self.imgpanel.SetVirtualSize( sz )

"sel" is the selected file name, "ext" is the extension from the file,
and "mapping" is a dictionary that maps the extension to a
wx.BITMAP_TYPE_XXXX. The imgpanel is a wx.ScrolledWindow, which lets me
handle images larger than the window.

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

Chris Barker

unread,
Jan 10, 2011, 11:39:55 PM1/10/11
to wxpytho...@googlegroups.com
On 1/10/2011 3:47 PM, JeffMGreg wrote:
> I've looked all over for this answer and I can't find it anywhere.
> Hope someone can help me with this. Basically, I want to find a way
> of changing an image whenever a button is clicked. I don't want to
> over write the image because sometimes the sizes will be different and
> that looks awful. Any help would be greatly appreciated.

Here's an example app that does what you want:

-Chris

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris....@noaa.gov

StaticBitmap.py

JeffMGreg

unread,
Jan 11, 2011, 12:15:36 AM1/11/11
to wxPython-users
Thank you so much Chris. I learned a lot of other things from your
example too.
> Chris.Bar...@noaa.gov
>
>  StaticBitmap.py
> 2KViewDownload
Reply all
Reply to author
Forward
0 new messages