All pastes #44260 Raw Edit

multibutton v2

public python v1 · immutable
#44260 ·published 2006-03-02 20:02 UTC
rendered paste body
import osfrom 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 = ()):        self.bits = {}        firstBitmap = self.AddStates(multibitmaps)        # assert firstBitmap is not None        print "Using firstBitmap of " + firstBitmap        super(MultiStateButton, 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(('%s.%s' % (os.path.join('/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)