Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change property parameter from within class constructor [Python / Traits]

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.

like image 748
Pteridium Avatar asked Mar 31 '12 13:03

Pteridium


People also ask

How do you pass a parameter to a constructor in Python?

Creating the constructor in pythonWe can pass any number of arguments at the time of creating the class object, depending upon the __init__() definition. It is mostly used to initialize the class attributes. Every class must have a constructor, even if it simply relies on the default constructor.

Is __ init __ constructor?

"__init__" is a reserved method in python classes. It is known as a constructor in OOP concepts. This method called when an object is created from the class and it allows the class to initialize the attributes of a class.

What is constructor and write 4 features of constructor in Python?

A constructor is a unique method used to initialize an object of the class. Python will provide a default constructor if no constructor is defined. In Python, we have three types of constructor default, Non-parametrized, and parameterized constructor.

How do you call a constructor from one class to another in Python?

We can use the super() function to call the superclass constructor function. We can also use the superclass name to call its init() method.


1 Answers

Finally found the answer in a recent mailing list message.

The code below works. It seems the devil is in the details of how Range() is called: Range(my_slider_low, my_slider_hi, 0.1) does not work.

from enthought.traits.api import HasTraits, Range
from enthought.traits.ui.api import View, Item

class GUIThing(HasTraits):

    my_slider_low = 0.0
    my_slider_hi = 1.0

    my_slider = Range(low='my_slider_low', high='my_slider_hi', value=0.1)  

    def __init__(self, arg1):
        self.my_slider_hi = arg1 

    view = View( Item('my_slider') )

top_range = 0.2

my_gui = GUIThing(top_range)
my_gui.configure_traits()
like image 63
Pteridium Avatar answered Sep 30 '22 19:09

Pteridium