Column widths with TabularAdapters?

牧云@^-^@ 提交于 2019-12-11 03:19:19

问题


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?


回答1:


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:



来源:https://stackoverflow.com/questions/29678405/column-widths-with-tabularadapters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!