All pastes #44256 Raw Edit

multibutton v2

public python v1 · immutable
#44256 ·published 2006-03-02 19:46 UTC
rendered paste body
from wx.lib.buttons import GenBitmapButtonclass MultiStateButton(GenBitmapButton):    """    A bitmap button that has more than one possible bitmap in its default state.    These bitmaps are passed in as a list of names, which are also the names of the PNG bitmaps.    foo = MultiStateButton(view, 100, multibitmaps=["MailState", "NoMailState"])    will create a button     """    kBitmapFileExtensions = ["png", "gif", "jpg", "tiff", "psd"]    def __init__(self, parent, ID, label="",                 pos = wx.DefaultPosition, size = wx.DefaultSize,                 style = 0, validator = wx.DefaultValidator,                 name = "multistatebutton", multibitmaps = ()):        firstBitmap = self.AddStates(multibitmaps)        self.bits = {}        # assert firstBitmap != None        print "Using firstBitmap of " + firstBitmap        super(GenBitmapButton, self).__init__(parent, ID, firstBitmap, pos, size, style, validator, name)    def AddStates(self, multibitmaps):        firstFound = None        for name in multibitmaps:            found = False            for ext in MultiStateButton.kBitmapFileExtensions:                try:                    img = wx.Image('/Users/rae/work/osaf/rae-button/' + name + '.' + ext)                    self.bits[name] = img.ConvertToBitmap()                    if not Found:                        firstFound = name                        Found = True                    break                except IOError:                    pass            if not found:                ##Reid                # should raise an error here somehow                pass        return firstFound    def SetState(self, state):        bitmap = self.bits.get(state)        if bitmap != None:            self.SetBitmapLabel(bitmap)