I'm new to python - sorry if my terminology is wrong. I have a class which inherits the Enthought Traits attributes. Here is a simplified version:
from enthought.traits.api import HasTraits, Range
from enthought.traits.ui.api import View, Item
class GUIThing(HasTraits):
my_slider = Range(0.0, 0.6, 0.1)
my_slider._high = 0.7 # works; not what I need 'coz not instance-dependent
view = View( Item('my_slider') )
def __init__(self, arg1):
# Call the parent's __init__
HasTraits.__init__(self)
self.my_slider._high = arg1 # what I need; doesn't work
# -- Main program -----
top_range = 0.9
my_gui = GUIThing(top_range)
my_gui.configure_traits()
This simply creates a window with a slider in it, originally going from 0.0 to 0.6 with initial value 0.1. When creating an instance of GUIThing I want to vary the maximum value for the slider depending on the current top_range
value. However the line
self.my_slider._high = arg1
results in
AttributeError: 'float' object has no attribute '_high'
When within __init__()
, self.my_slider
returns not the slider object, but the current value of the slider.
What am I doing wrong? Thanks!
Edit:
The following also doesn't work:
class GUIThing(HasTraits):
def __init__(self, arg1):
# Call the parent's __init__
HasTraits.__init__(self)
self.my_slider = Range(0.0, arg1, 0.0)
view = View( Item('my_slider') )
That would be the direct way to do what I'm trying to do, but it results in a GUI where instead of a slider, there is a text box that reads "enthought.traits.trait_types.Range object at 0xa61946c". So the problem is that when my_slider
is created within __init__()
then "my_slider" comes to mean the object itself (which doesn't display properly via View); but if my_slider
is created outside of __init__()
then "my_slider" comes to mean the current value (a float; which prevents access to the object properties).
Not sure if this is peculiar to Traits or I just don't know how to initialise objects properly.