wxpythonsizer

WxPythonAdd List to Grid Sizer


I have a list of images (wxImg) which I want to add to a grid sizer. I  cannot figure it out how to add them. Here is my piece of code.

import wx

class MyPanel(wx.Panel):
    def __init__(self, *args, **kwargs):
        wx.Panel.__init__(self, *args, **kwargs)
        msz = wx.BoxSizer(wx.VERTICAL)
        gs = wx.GridSizer(5, 5, 0, 0)
        self.ic = []
        for i in range(len(wxImg)):
            self.ic.append(wx.StaticBitmap(self, wx.ID_ANY,
                           wx.BitmapFromImage(wxImg[i]),
                           name="Pic" + str(i + 1))
            gs.Add(self.ic[i], 0, wx.ALIGN_LEFT)
            self.ic[i].Bind(wx.EVT_LEFT_DOWN, self.OnClick)
            msz.Add(gs, 0, wx.ALL, 5)
            self.SetSizer(msz)

and so on. It always gives me error. Please help.

Thanks.


Solution

  • You forgot to provide what type of error it gives you. It can be anything, it might even be something not related to what you're doing, so please provide error information. Also what does "wxImg" hold? wx.Images? path to the images?

    Here is something I have done that is similar. I modified it a bit. Orignial is here

    image_grid = wx.FlexGridSizer(rows=3, cols=2, vgap=10, hgap=10)
    image_list = ['add.jpg', 'subtract.jpg', 'divide.jpg',
                      'multiply.jpg', 'mix.jpg', 'custom.jpg']
    
        for image in image_list:
            bmp = wx.Image(img_dir.format(image),
                           wx.BITMAP_TYPE_ANY).ConvertToBitmap()
            bitmap = wx.StaticBitmap(self, -1, bmp)
    
            image_grid.Add(bitmap)