I have a case in which I don't know the required contents (i.e. - set of Traits) of a HasTraits subclass, until program run time, because it depends upon the result of parsing a certain file with variable contents.
How can I customize the View for this HasTraits subclass programmatically, just before calling its configure_traits() method?
Here is a simple test case, which illustrates the problem:
#! /usr/bin/env python
'Test case, showing problem with dynamically constructed view.'
from traits.api import HasTraits
from traitsui.api import View, Item
class DynamicViewTester(HasTraits):
'Tries to dynamically construct its View, using default_traits_view().'
def default_traits_view(self):
view = View(
Item(label='Hello, World!'),
title='Dynamically Assembled View',
)
view.add_trait('msg', Item(label='Goodbye, World.'))
return view
if(__name__ == '__main__'):
DynamicViewTester().configure_traits()
When I run this code, I only see the "Hello, World!" message in the resultant GUI. I do NOT see the "Goodbye, World." message.
I found a solution: