RangeEditorは浮動小数点の特性を期待するために配線されているようです。次のサンプルスクリプトは動作を示しています。整数特性に対してRangeEditorを指定するよりクリーンな方法はありますか? RangeEditor
整数のための予期しないRangeEditor
import sys
print (sys.version) # '3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64 bit (AMD64)]'
from traits import __version__
print (__version__) # '4.6.0.dev0'
from traits.api import HasTraits, Int, Range
from traitsui.api import Item, RangeEditor, View
class Exposure (HasTraits):
duration = Range (low=0.0, high=3600.0, exclude_low=True , exclude_high=False, editor=RangeEditor(mode='slider'))
min_count = Int ( 1)
max_count = Int (10000)
#<1>count = Range (low=1 , high=10000 , exclude_low=False, exclude_high=False, editor=RangeEditor(mode='slider'))
count = Range (low=1 , high=10000 , exclude_low=False, exclude_high=False, editor=RangeEditor(mode='slider', low_name='min_count', high_name='max_count'))
traits_view = View (Item('duration'), Item('count'), buttons=['OK', 'Cancel'])
exposure = Exposure (duration = 0.00032, count=1500)
exposure.configure_traits()
# Specifying `count` as per <1>, above, raises the following exception:
# traits.trait_errors.TraitError: The 'count' trait of an Exposure instance must be 1 <= a long integer <= 10000, but a value of 1.0 <class 'float'> was specified.
#
# A work-around is to declare min_count and max_count, and include them in the
# RangeEditor definition of `count`. Note, I am now duplicating `count` low and
# high limits in 2 places. Is there a better approach?
感謝。私は本当に 'count'を制約を適用する範囲にしたいと思っています。また、 'duration'のItem指定には、低い値と高い値を含める必要があります。それ以外の場合、RangeEditorは[0.0、1.0]のスライダを表示します。私はMVCを理解していますが、(モデル)特性宣言と(ビュー)RangeEditor宣言(DRY IMHOの違反)の両方で、Range lowとhighを指定する必要がありますが、私はそれらを(モデル)クラス定義。 –
OK。 'count'をさらに変更することなく' Range'特性に変更することは問題ではありません。私はあなたのDRY不快感に同意する傾向があります。私は現在の行動が意図的だと思うが、確かにそれを知らない。 –
私はちょうど同じ問題に直面しています。 RangeEditorは、編集された特性が整数か浮動かを判断するのに十分なほど賢明でなければならないと私は同意します。 –