使用Enthought Canopy时出现configure_traits问题



我正在学习教程"使用TraitsUI为科学编程编写图形应用程序"http://code.enthought.com/projects/traits/docs/html/tutorials/traits_ui_scientific_app.html并测试了以下代码片段:

from enthought.traits.api import *
from enthought.traits.ui.api import *
class Camera(HasTraits):
    """ Camera object """
    gain = Enum(1, 2, 3,
        desc="the gain index of the camera",
        label="gain", )
    exposure = CInt(10,
        desc="the exposure time, in ms",
        label="Exposure", )
    def capture(self):
        """ Captures an image on the camera and returns it """
        print "capturing an image at %i ms exposure, gain: %i" % (
            self.exposure, self.gain )
if __name__ == "__main__":
    camera = Camera()
    camera.configure_traits()
    camera.capture()

如果我在命令行运行这个,它会像广告中所说的那样工作。一个GUI弹出。您可以调整参数,当您单击"确定"时,它会返回修改后的值。但是,当我在Canopy编辑器中单击运行按钮运行相同的代码时,默认参数会立即打印出来;然后窗口弹出。当您调整GUI中的参数并单击"确定"时,GUI将退出,但不会打印新的参数值。

就好像camera.capture()在camera.configuration _ traits.之前运行一样

首先,我建议使用本教程的新版本:http://docs.enthought.com/traitsui/tutorials/traits_ui_scientific_app.html

您链接到的是TraitsUI版本3的参考资料,而上面的是您可能使用的版本(版本4)。较新的教程使用较新的模块名称,例如traitsui.api而不是enthought.traits.ui.api

至于为什么Canopy会立即显示值,这是运行程序时的预期行为:

if __name__ == "__main__":
    camera = Camera()
    camera.configure_traits()
    camera.capture()

当以__main__运行时(即,不是由另一个脚本作为模块导入),脚本按顺序执行以下三件事:创建Camera()的实例,弹出GUI(configure_traits),然后执行打印当前值(默认为"1"one_answers"10")的capture方法。

OK(确定)/Cancel(取消)按钮不用于设置这些值。作为测试,请尝试更改曝光或增益,但不要单击按钮,而是在GUI仍然打开时,尝试从Canopy的IPython提示中检查这些属性:camera.gaincamera.exposure应返回新设置的值。

最新更新