Search code examples
enthoughtcanopytraitsui

Column widths with TabularAdapters?


Using Enthought Canopy's TraitsUI, I'm using TabularAdapters to display some Arrays, but they always produce evenly proportioned column widths...I'd like to make some widths smaller than others, but haven't found any simple way yet...Anyone have any suggestions?


Solution

  • One way to control the widths of the columns is to override the get_width() method of the TabularArrayAdapter. For example,

    import numpy as np
    from traits.api import HasTraits, Array
    from traitsui.api import View, Item, TabularEditor
    from traitsui.tabular_adapter import TabularAdapter
    
    
    test_dtype = np.dtype([('x', 'int'),
                           ('y', 'int'),
                           ('r', 'float')])
    
    
    class TestArrayAdapter(TabularAdapter):
    
        columns = [(name, idx) for idx, name in enumerate(test_dtype.names)]
    
        even_bg_color = 0xF0F4FF
    
        def get_width(self, object, name, col):
            widths = {0: 50, 1: 50, 2: 150}
            return widths[col]
    
    
    class Test(HasTraits):
    
        array1 = Array(dtype=test_dtype)
    
        view = \
            View(
                Item(name='array1', show_label=False,
                     editor=TabularEditor(adapter=TestArrayAdapter())),
                resizable=True,
            )
    
    
    a1 = np.array([(10, 20, 1.5), (15, 31, 2.4), (14, 11, 1.9), (21, 13, 2.5)],
                  dtype=test_dtype)
    test = Test(array1=a1)
    test.configure_traits()
    

    Screenshot:

    screenshot