I'm working on a by-name colour chooser, something that lets you choose a
colour from a list of names will little colour swatches (icons) off to the
side. I build the colour swatches from Numeric arrays then convert to
Image, then bitmap, and add them to the image list for the object. The code
seems fine (it's not finished, of course, but no errors/exceptions raised),
except that the icons are not actually showing up in the list control.
Notes:
wxPython 2.3.0 (downloaded minutes ago), 2.2.5 has same result
In the code, I'm keeping an explicit reference to each bitmap in a
dictionary, doesn't seem to help at all.
Bitmaps return Ok() as true.
There's no way I've yet found to convert to wxIcon from wxBitmap, so I
can't go that way .
You'll need to modify your (magic)imagelist to allow wxBitmapPtrs if you
want to see it running at all.
wxImage.ConvertToBitmap() and wxBitmapFromImage() both return a wxBitmapPtr
instance, not a wxBitmap, not sure if that's normal, it requires a change to
the imagelist mixin regardless, as that checks for isinstance( wxBitmap,
icon ).
Any suggestions would be appreciated,
Mike
8<____________ colorlistcontrol.py _______
from wxPython.wx import *
from Numeric import *
from wxcontrols import magicimagelist
class ColorListControl( wxListCtrl, magicimagelist.MagicImageList ):
"""The color list control allows you to select
from a list of (predefined) colours by name and
a colour swatch (icon)"""
def __init__(
self, parent, id=-1,
pos = wxDefaultPosition, size = wxDefaultSize,
style = wxLC_SMALL_ICON , validator = wxDefaultValidator,
name = "colorlist",
):
wxListCtrl.__init__( self, parent, id, pos, size, style, validator,
name )
self.items = {}
self.bitmaps = {}
self.SetupIcons( )
self.GetInitialPopulation()
def AddNamedColor( self, name, (r,g,b) ):
"""Add a single named color to the items and the display"""
normal, selected = self.GetIcons( (r,g,b) )
self.InsertImageStringItem( self.GetItemCount(), name, normal )
def GetInitialPopulation( self ):
"""Populate with the default colours"""
## import csscolors
## items = csscolors.colors.items()
## items.sort()
items = [("black", (0,0,0)), ("blue", (0,0,255))]
for key, value in items:
self.AddNamedColor( key, value )
def GetIcon( self, (r,g,b) ):
if self.bitmaps.get( (r,g,b)):
return self.bitmaps.get( (r,g,b))
data = zeros( (self.DEFAULTICONSIZE, self.DEFAULTICONSIZE, 3), "c")
data[:,:] = (chr(r), chr(g), chr(b))
image = wxEmptyImage( self.DEFAULTICONSIZE, self.DEFAULTICONSIZE )
image.SetData( data.tostring())
bitmap = wxBitmapFromImage( image ) #image.ConvertToBitmap()
self.bitmaps[ (r,g,b) ] = bitmap
print 'bitmap ok', bitmap.Ok()
return bitmap
if __name__ == "__main__":
class TestFrame(wxFrame):
def __init__( self ):
wxFrame.__init__( self, NULL, -1, "Test", size = (500,500) )
ColorListControl( self )
class App( wxPySimpleApp):
def OnInit( self ):
TestFrame().Show( 1)
return 1
App().MainLoop()