Question

I have a simple program that gets an text input from the user, and displays it on an instrument (Keithley). However, the attribute does not seem to change. That is, when I run the Start method, the output is "Wowee", even if I change Display in a pop-up window. Do I have to make it editable? I didn't think so. I am basically following Gael Varoquaux's intro to traits.

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()")
Was it helpful?

Solution

In the ControlPanel class, you have created two different instances of Keithley3706ASettings, namely keithley3706Asettings and k3706A_settings. If I delete the latter, and replace the use of k3706A_settings with keithley3706Asettings, it works. Here's my version (with references to the 2430 device removed, the k3706A trait declared as an Instance of Keithley3706A, and a few other irrelevant UI changes):

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()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top