Search code examples
pythonwxpythonwxwidgets

what is the logic to toggle toolbar widget 4 times one after the other in wxpython?


Here, I am trying to change bitmap image on toolbar click,

img = wx.Image('Image_A Path Here').Scale(50, 33, wx.IMAGE_QUALITY_HIGH)
face_tool = self.toolbar.AddTool(6, 'Animation', wx.Bitmap(img), 'Change Face Image')
self.on_face_click(None, init_face=True)
self.Bind(wx.EVT_TOOL, self.on_face_click, face_tool)

def on_face_click(self, event, init_face=False):
    if init_face:
        self.Test_face_click = False
    else:
        self.Test_face_click = not self.Test_face_click
    if self.Test_face_click:
        img = "Image_B Path Here"
        png = wx.Image(img, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
        self.bottomToolbar.SetToolNormalBitmap(id=6, bitmap=png)
    else:
        img = "Image_A Path Here"
        png = wx.Image(img, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
        self.bottomToolbar.SetToolNormalBitmap(id=6, bitmap=png)

I want to change bitmap image 4 times one after the other on every click event.

  • Suppose default image is image_A then on 1st click (on_face_click) event I would like to change bitmap as image_B, then on 2nd click it should change to image_C, then on 3rd click change it to image_C ,then on 4rd click change it to image_A again etc.
  • Repeat in loop.

Please help me to resolve this logic.


Solution

  • self.btnImages= [ 'path of image_B', 'path of image_C','path of image_D','path of image_A']
    self.img_index = 0
    img = wx.Image('Image_A Path Here').Scale(55, 35, wx.IMAGE_QUALITY_HIGH)
    b_face_tool = self.bottomToolbar.AddTool(id=6, 'Animation', wx.Bitmap(img))
    self.Bind(wx.EVT_TOOL, self.on_face, b_face_tool)
    
    def on_face(self, event):
        if (self.img_index == len(self.btnImages)):
            self.img_index = 0
        img = self.btnImages[self.img_index]
        self.bottomToolbar.SetToolNormalBitmap(id=6, bitmap=wx.Image(img, wx.BITMAP_TYPE_ANY).ConvertToBitmap())
        self.img_index = self.img_index + 1