Using Custom ArtProvider

I am using version 2.8.12.1 and I am also borrowing from the “wxPython 2.8 Cookbook”.

I want to add my own images to the art provider. I pasted the relevant code below. I overwrite the CreateBitmap function, but it looks like it doesn’t even get called. I’m not sure how to go about fixing this. I can’t find any examples of people actually using a custom ArtProvider, only examples of overwriting CreateBitmap.

Code:

class CustomArtProvider(wx.ArtProvider):

    image_dir = myApp.image_dir

    def __init__(self):
        super(CustomArtProvider, self).__init__()

        # Attribs
        self.bmps = [os.path.splitext(bmp)[0]
                     for bmp in os.listdir(self.image_dir)
                     if bmp.endswith(".png") or bmp.endswith(".jpg")]
        # print self.bmps #  ['Stop', 'Play']

        def CreateBitmap(self, id,
                         client=wx.ART_OTHER,
                         size=wx.DefaultSize):
            print "Create Bitmap"  # never called
            # Return NullBitmap on GTK to allow the default
            # artprovider to get the system theme bitmap.
            if wx.Platform == '__WXGTK__':
                return wx.NullBitmap

            # Non GTK Platform get custom resource
            # when one is available.
            if id in self.bmps:
                path = os.path.join(self.image_dir, id+'.png')
                bmp = wx.Bitmap(path)

            return bmp


# In my frame
def AddBasicTool(self, toolbar, label, description, imgId, handler):
    """Add a basic tool to a given toolbar."""
    img = wx.ArtProvider.GetBitmap(imgId, size=self.ICON_SIZE)
    tool = toolbar.AddSimpleTool(-1, img, label, description)
    self.Bind(wx.EVT_MENU, handler, tool)

def __CreateToolBar(self):
    """Creates and sets up a toolbar."""
    toolbar = self.CreateToolBar()

    self.AddBasicTool(toolbar, "Load", "Load RFview Scenario",
                      wx.ART_FILE_OPEN, self.OnLoadScenario)  # WORKS AS EXPECTED
    self.AddBasicTool(toolbar, "Load", "Load RFview Scenario",
                      "Play", self.OnLoadScenario)  # NOTHING DISPLAYS

# My App
class MainApp(wx.App):
    def OnInit(self):
        # Push a custom ArtProvider onto the provider stack.
        wx.ArtProvider.PushProvider(CustomArtProvider())
        # Setup out main frame
        self.frame = MainFrame(None)
        self.frame.Show()
        return True

CreateBitmap was over indented so it was in the init. Stupid mistake on my part.

···

On Tuesday, August 2, 2016 at 11:52:25 AM UTC-4, Noah wrote:

I am using version 2.8.12.1 and I am also borrowing from the “wxPython 2.8 Cookbook”.

I want to add my own images to the art provider. I pasted the relevant code below. I overwrite the CreateBitmap function, but it looks like it doesn’t even get called. I’m not sure how to go about fixing this. I can’t find any examples of people actually using a custom ArtProvider, only examples of overwriting CreateBitmap.

Code:

class CustomArtProvider(wx.ArtProvider):

    image_dir = myApp.image_dir

    def __init__(self):
        super(CustomArtProvider, self).__init__()

        # Attribs
        self.bmps = [os.path.splitext(bmp)[0]
                     for bmp in os.listdir(self.image_dir)
                     if bmp.endswith(".png") or bmp.endswith(".jpg")]
        # print self.bmps #  ['Stop', 'Play']

        def CreateBitmap(self, id,
                         client=wx.ART_OTHER,
                         size=wx.DefaultSize):
            print "Create Bitmap"  # never called
            # Return NullBitmap on GTK to allow the default
            # artprovider to get the system theme bitmap.
            if wx.Platform == '__WXGTK__':
                return wx.NullBitmap

            # Non GTK Platform get custom resource
            # when one is available.
            if id in self.bmps:
                path = os.path.join(self.image_dir, id+'.png')
                bmp = wx.Bitmap(path)

            return bmp


# In my frame
def AddBasicTool(self, toolbar, label, description, imgId, handler):
    """Add a basic tool to a given toolbar."""
    img = wx.ArtProvider.GetBitmap(imgId, size=self.ICON_SIZE)
    tool = toolbar.AddSimpleTool(-1, img, label, description)
    self.Bind(wx.EVT_MENU, handler, tool)

def __CreateToolBar(self):
    """Creates and sets up a toolbar."""
    toolbar = self.CreateToolBar()

    self.AddBasicTool(toolbar, "Load", "Load RFview Scenario",
                      wx.ART_FILE_OPEN, self.OnLoadScenario)  # WORKS AS EXPECTED
    self.AddBasicTool(toolbar, "Load", "Load RFview Scenario",
                      "Play", self.OnLoadScenario)  # NOTHING DISPLAYS

# My App
class MainApp(wx.App):
    def OnInit(self):
        # Push a custom ArtProvider onto the provider stack.
        wx.ArtProvider.PushProvider(CustomArtProvider())
        # Setup out main frame
        self.frame = MainFrame(None)
        self.frame.Show()
        return True