为什么属性不会在用户输入的traitsui中更新

时间:2013-11-02 02:04:56

标签: python enthought traitsui

我有一个简单的程序,可以从用户那里获取文本输入,并将其显示在乐器上(Keithley)。但是,该属性似乎没有改变。也就是说,当我运行Start方法时,即使我在弹出窗口中更改了Display,输出也是“Wowee”。我必须让它可编辑吗?我不这么认为。我基本上跟随Gael Varoquaux's介绍特征。

COntrolPanel.py:

from traits.api import *
from traitsui.api import *

import Keithley3706A_module



class ControlPanel(HasTraits):
    keithley2430settings = Instance(Keithley2430.Keithley2430Settings, ())
    keithley3706Asettings = Instance(Keithley3706A_module.Keithley3706ASettings, ())
    start = Button("Start Measurements")
    clear_3706A_display = Button("Clear K3706A Display")

    k3706A_settings = Keithley3706A_module.Keithley3706ASettings()
    k3706A = Keithley3706A_module.Keithley3706A()
    view = View(Item('start', show_label=False,style='custom' ),
        Item('clear_3706A_display', show_label=False,style='custom' ),
        Item('keithley2430settings',style='custom'),
        Item('keithley3706Asettings',style='simple'))


    def _start_fired(self):
        print "hello %s" % self.k3706A_settings.display
        self.k3706A.message(self.k3706A_settings.display)

    def _clear_3706A_display_fired(self):
        self.k3706A.clear()

if __name__ == '__main__':
    ControlPanel().configure_traits()

Keithley3706A.py:

from traits.api import *
from traitsui.api import *

import visa
import time

class Keithley3706ASettings(HasTraits):
    display = String("Wowee")

class Keithley3706A(HasTraits):
    def __init__(self):
       self.Keithley3706AUSB = visa.instrument("USB0::0x05E6::0x3706::04019447::INSTR") 

    def message(self,foo):
        s="display.settext('%s')" % foo
        self.Keithley3706AUSB.write("display.clear()")
        self.Keithley3706AUSB.write(s)

    def clear(self):
        self.Keithley3706AUSB.write("display.clear()")

1 个答案:

答案 0 :(得分:2)

ControlPanel课程中,您创建了两个Keithley3706ASettings的不同实例,即keithley3706Asettingsk3706A_settings。如果我删除后者,并将k3706A_settings替换为keithley3706Asettings,则可行。这是我的版本(删除了对2430设备的引用,k3706A Instance Keithley3706A的{​​{1}}特征,以及其他一些不相关的UI更改):

class ControlPanel(HasTraits):

    keithley3706Asettings = Instance(Keithley3706A_module.Keithley3706ASettings, ())

    k3706A = Instance(Keithley3706A_module.Keithley3706A, ())

    start = Button("Start Measurements")
    clear_3706A_display = Button("Clear K3706A Display")

    view = View(UItem('start'),
                UItem('clear_3706A_display'),
                Item('keithley3706Asettings', style='simple'))

    def _start_fired(self):
        print "hello %s" % self.keithley3706Asettings.display
        self.k3706A.message(self.keithley3706Asettings.display)

    def _clear_3706A_display_fired(self):
        self.k3706A.clear()