简介
Traits是开源扩展库,Traits本身与科学计算可视化没有直接关联,但他其实TVTK,Mayavi,TraitsUI基础
安装:
pip3 install traits-4.6.0-cp36-cp36m-win_amd64.whl
pip3 install traits
from traits.api import HasTraits 进行测试
使用:
traits属性可以像类的属性来定义,但是要像实例的属性来使用
>>> from traits.api import HasTraits
>>> from traits.api import HasTraits,Color
>>>
>>> class Circle(HasTraits):
... color = Color #traits属性可以像类的属性来定义,但是要像实例的属性来使用
...
>>> c = Circle()
>>> Circle.color #不是类的属性
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: type object 'Circle' has no attribute 'color'
>>> c.color #是实例的属性
<PyQt4.QtGui.QColor object at 0x000000000AAA74A8>
>>> c.color = 'red'
>>> c.color.getRgb()
(255, 0, 0, 255)
>>> c.color = 'abc' #按照严格的类型来区别变量,并且会给出明确的错误信息和使用提示
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-p
ackages\traits\trait_handlers.py", line 173, in error
value )
traits.trait_errors.TraitError: The 'color' trait of a Circle instance must be a
string of the form (r,g,b) or (r,g,b,a) where r, g, b, and a are integers from
0 to 255, a QColor instance, a Qt.GlobalColor, an integer which in hex is of the
form 0xRRGGBB, a string of the form #RGB, #RRGGBB, #RRRGGGBBB or #RRRRGGGGBBBB
or 'aliceblue' or 'antiquewhite' or 'aqua' or 'aquamarine' or 'azure' or 'beige'
or 'bisque' or 'black' or 'blanchedalmond' or 'blue' or 'blueviolet' or 'brown'
.............
>>> c.configure_traits() #出现配置属性框,供我们选择颜色
>>> c.configure_traits()
True #选择颜色后返回True
>>> c.color
<PyQt4.QtGui.QColor object at 0x000000000EA74D68> #现在返回的颜色与我们选择的一致