On Tue, Mar 4, 2014 at 9:17 AM, Nouvelle Collection <
[email protected]> wrote:

> Hello,
> (This is my last question for the week, sorry for all these mails).
>
> I would like to change the image of a ScaledBitmap when clicking on it :
>
> A = self.Canvas.AddScaledBitmap(img, (0,0), Height=img.GetHeight(),
> Position = 'tl')
> A.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.OnDown)
>
> def OnDown(self, elt):
>     # ...
>     # Change A's image to another bitmap
>
> *The goal is to make a ToggleButton : each click on the button would
> toggle between img1 and img2.*
>
> Remark : I already implemented it by doing :
> A_enabled = self.Canvas.AddScaledBitmap(img_ena, (0,0),
> Height=img_ena.GetHeight(), Position = 'tl')
> A_disabled = self.Canvas.AddScaledBitmap(img_dis, (0,0),
> Height=img_dis.GetHeight(), Position = 'tl')
> and then each click on both of them would *hide one of them, and show the
> other*...
> But this is a kind of dirty way to do it...
>

Well, that's not so bad....

But the semi-"right" way to do it would be to change the Image in place:

def OnDown(self, elt):
    # ...
    # Change A's image to another bitmap
          elt.Image = the_new_image

This should "just work", but only if:
   the_new_image is a wx.Image object
   it is the same size as the previous image.


But the "right" way to do it is to make a ScaledBitmap that stored two
images. You could subclass ScaledBitmap:

class ToggleScaledBitmap(FloatCanvas.ScaledBitmap):
    def__init__(self, Bitmap1, Bitmap2, *args, **kwargs):

        if type(Bitmap1) == wx._gdi.Bitmap:
            self.Image1 = Bitmap1.ConvertToImage()
        elif type(Bitmap) == wx._core.Image:
            self.Image1 = Bitmap1
        ## repeated code -- should be cleaned up...
        if type(Bitmap2) == wx._gdi.Bitmap:
            self.Image2 = Bitmap1.ConvertToImage()
        elif type(Bitmap) == wx._core.Image:
            self.Image2 = Bitmap2

       ## some checks in here to make sure that the two bitmaps are the
same size...

        FloatCanvas.ScaledBitmap.__init__(self, self.Image1, *args,
**kwargs)

    def ChangeBitmap(self, bitmap_num=1):
        if bitmap_num == 1:
            self.Image = self.Image1
        elif bitmap_num == 2:
            self.Image = self.Image2
        else:
            raise ValueError("bitmap_num ust be either 1 or 2")

All untested....

Better yet, make it a MultiScaledBitmap, and allow the user to pass in a
list of Images of any length, and switch between them.

This would be nice to add to FloatCanvas once you get it working....

-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

[email protected]
_______________________________________________
FloatCanvas mailing list
[email protected]
http://mailman.paulmcnett.com/cgi-bin/mailman/listinfo/floatcanvas

Reply via email to