Question

I wonder how I might create a solid box of color for my python traits GUI which I can change the color of by, say, clicking on different buttons.

I've found the ColorEditor editor, so I can achieve solid boxes of color by defining a trait:

 my_color_box = Color()

and then in my traits view definition:

 Item('my_color_box', editor=ColorEditor(),style='readonly'),

However, the box also contains text with the color name, which isn't the look I want. I've tried the other styles of the ColorEditor() and none seem to give me a solid box of color.

Does anyone know how I could achieve this please?

Thanks,

Was it helpful?

Solution

This isn't natively dealt with in a traitsui editor as far as I can see. The easiest thing to do (depending on what you want to do) is to just use solid images of the desired color (with ImageEditor). Even if you have several different possibilities for colors that you'd like to flip between, an ImageEnumEditor (in readonly style) could capture them.

To capture the expressive power of traits being able to capture any arbitrary color (without enumerating the list of 256^3 possible colors which I don't recommend), you would need to a bit more work. Probably, you could define a custom editor that delves into the toolkit code to do this without too much effort. I was going to try to provide a minimal working example using wxpython, but I didn't find a super-obvious widget to do this with in wxpython and my wxpython skills are pretty marginal.

Edit:

I found a way to generate a table with colored boxes in it a year ago. Sorry I didn't think of it earlier, it's pretty hacky if you don't actually want a table mapping to colors (which is what I did want), so I bet you could construct something simpler using the traitsui and not the wx internals. But anywhere, here's something, in the spirit of trying to give you tools to help solve your own problem:

from traits.api import *
from traitsui.api import *

class ColorColumn(ObjectColumn):
  def get_cell_color(self,object):
    return object.color

class ColorContainer(HasTraits):
  color=Color('red')
  blank_text=Str('')

class SomeApplication(HasTraits):
  dummy_table=List(ColorContainer)

  def _dummy_table_default(self):
    return [ColorContainer()]

  traits_view=View(Item(name='dummy_table',
    editor=TableEditor(columns=
      [ColorColumn(label='',editor=TextEditor(),name='blank_text',editable=False)],
      selection_bg_color=None,),show_label=False))

SomeApplication().configure_traits()

Edit2:

As you've asked for, here's a minimal working example using ImageEnumEditor or ImageEditor. In this example, the images are located at /path_to_the_python_file/images. Note that ImageEnumEditor only works with .gif files.

$ ls images
green.gif red.gif yellow.gif

from traits.api import *
from traitsui.api import *
from pyface.image_resource import ImageResource

class ImageEnumStyle(HasTraits):
  ci=Enum('yellow','green','red','yellow')

  traits_view=View(Item('ci',editor=ImageEnumEditor(path='images',),style='readonly'))

class ImageStyle(HasTraits):
  ci=Instance(ImageResource)

  #to modify the image, modify the ImageResource `name` attribute
  def _ci_default(self):
    return ImageResource('yellow.gif')

  traits_view=View(Item('ci',editor=ImageEditor()))

ImageWhicheverStyleYouPrefer().configure_traits()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top