1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | ########### SVN repository information ################### |
---|
4 | # $Date: 2012-04-22 23:49:05 +0000 (Sun, 22 Apr 2012) $ |
---|
5 | # $Author: jemian $ |
---|
6 | # $Revision: 814 $ |
---|
7 | # $URL$ |
---|
8 | # $Id: traits_probe.py 814 2012-04-22 23:49:05Z jemian $ |
---|
9 | ########### SVN repository information ################### |
---|
10 | |
---|
11 | import argparse |
---|
12 | import epics |
---|
13 | import sys #@UnusedImport |
---|
14 | |
---|
15 | choose_Qt4 = True |
---|
16 | #choose_Qt4 = False |
---|
17 | from traits.etsconfig.api import ETSConfig |
---|
18 | if choose_Qt4: |
---|
19 | ETSConfig.toolkit = 'qt4' |
---|
20 | else: |
---|
21 | ETSConfig.toolkit = 'wx' |
---|
22 | |
---|
23 | from traits.api import * #@UnusedWildImport |
---|
24 | from traitsui.api import * #@UnusedWildImport |
---|
25 | |
---|
26 | from traitsui.key_bindings \ |
---|
27 | import KeyBinding, KeyBindings |
---|
28 | |
---|
29 | |
---|
30 | key_bindings = KeyBindings( |
---|
31 | KeyBinding( binding1 = 'Enter', |
---|
32 | description = 'Connect to new PV', |
---|
33 | method_name = 'connect' ), |
---|
34 | ) |
---|
35 | |
---|
36 | |
---|
37 | # TraitsUI Handler class for bound methods |
---|
38 | class CodeHandler ( Handler ): |
---|
39 | |
---|
40 | def connect ( self, info ): |
---|
41 | if len(info.object.name) > 0: |
---|
42 | try: |
---|
43 | if info.object._chid is not None: |
---|
44 | info.object._chid.cancel_callback() |
---|
45 | info.object._chid.disconnect() |
---|
46 | except: |
---|
47 | pass |
---|
48 | info.object._chid = epics.PV(str(info.object.name), |
---|
49 | callback=info.object.do_callback) |
---|
50 | |
---|
51 | |
---|
52 | class Probe( HasTraits ): |
---|
53 | name = Str |
---|
54 | value = Str |
---|
55 | _chid = Instance( epics.PV, value = None ) |
---|
56 | |
---|
57 | def do_callback(self, value=None, **kwds): |
---|
58 | "simple monitor callback" |
---|
59 | self.value = str(value) |
---|
60 | |
---|
61 | traits_view = View( |
---|
62 | Item('name'), |
---|
63 | Readonly('value'), |
---|
64 | resizable=True, |
---|
65 | width = 250, |
---|
66 | title='Traits-based EPICS Probe', |
---|
67 | key_bindings = key_bindings, |
---|
68 | handler = CodeHandler() |
---|
69 | ) |
---|
70 | |
---|
71 | if __name__ == '__main__': |
---|
72 | #pv = '' |
---|
73 | #if len(sys.argv) == 2: |
---|
74 | # pv = sys.argv[1] |
---|
75 | parser = argparse.ArgumentParser(description="traits_probe") |
---|
76 | parser.add_argument('pv', action='store', nargs='?', help="EPICS PV name", default="EpicsDemo1") |
---|
77 | results = parser.parse_args() |
---|
78 | |
---|
79 | probe = Probe(name=results.pv) |
---|
80 | probe.configure_traits() |
---|