2013-11-25 47 views
0

,當我按照教程「編寫使用TraitsUI http://code.enthought.com/projects/traits/docs/html/tutorials/traits_ui_scientific_app.html 科學編程圖形應用程序和測試下面的代碼片段:問題與configure_traits使用Enthought雨棚

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將退出,但新參數eter值不打印。

就好像某種方式c​​amera.capture()在camera.configure_traits之前運行。 http://docs.enthought.com/traitsui/tutorials/traits_ui_scientific_app.html

你鏈接到引用材料TraitsUI 3版本之一,而上面的一個是版本你有可能使用(第4版:

回答

1

首先,我會建議使用本教程的這個新版本)。較新的教程使用較新的模塊名稱,例如traitsui.api而不是enthought.traits.ui.api

至於爲什麼雨棚立即顯示的數值,這是運行程序時的預期的行爲:

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

當作爲__main__(即,不導入由另一個腳本模塊),腳本運行這三件事按順序:創建一個Camera()實例,彈出GUI(configure_traits),然後執行打印當前值的方法(默認爲「1」和「10」)。

確定/取消按鈕沒有掛在設置這些值。作爲測試,嘗試更改暴露或增益,但不要單擊按鈕,嘗試在GUI仍處於打開狀態時從Canopy的IPython提示中檢查這些屬性:camera.gaincamera.exposure應返回新設置的值。