-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirstcalc.py
More file actions
106 lines (88 loc) · 3.07 KB
/
Copy pathfirstcalc.py
File metadata and controls
106 lines (88 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python3
"""Example tab showing the common TraitsUI widgets wired to a live plot.
Each trait below maps to a control in :attr:`FirstCalc.view`; the
``_<name>_changed`` and ``_<name>_fired`` methods are the notifications Traits
calls automatically when a value changes or a button is pressed.
"""
import os
import random
import numpy as np
from traits.api import (
Bool,
Button,
Directory,
Enum,
Float,
HasTraits,
Int,
Range,
Str,
)
from traitsui.api import Group, Item, View
class FirstCalc(HasTraits):
"""Demonstration panel driving the shared figure owned by the main window."""
# Add Traits objects
option = Bool(True)
rangex = Range(1, 10, 5)
yval = Float()
listoptions = Enum("Option 1", "Option 2", "Option 3")
stringopt = Str("Default string which can also be empty.")
stringoptread = Str("Default string which can also be empty [read-only version].")
masterpath = Directory()
button1 = Button("Button 1 Name")
floatval = Float()
changedcount = Int()
plotbutton = Button("Plot Me!")
yintcept = Range(0.0, 5.0, 0.0)
gradient = Range(0.0, 5.0, 1.0)
# Construct the view
view = View(
Item(name="rangex", label="X-Value"),
Item(name="yval", style="readonly", label="1/x", format_str="%.2e"),
Item(name="listoptions", label="list of options"),
Item(name="stringopt"),
Item(name="stringoptread", style="readonly"),
Item(name="masterpath", label="Directory"),
Group(Item(name="button1", show_label=False)),
Group(
Item(name="option", label="Boolean Option"),
enabled_when="floatval < 0.5",
label="Enabled Area Upon Random Value < 0.5",
show_border=True,
),
Item(name="changedcount", style="readonly", label="Attempts"),
Item(name="floatval", label="random generated"),
Group(
Item(name="gradient", label="gradient"),
Item(name="yintcept", label="y-intercept"),
Item(name="plotbutton", show_label=False),
show_border=True,
label="Plotting Area",
),
)
def __init__(self, main, **kwargs):
super().__init__(**kwargs)
self.main = main
self.yval = 1.0 / self.rangex
def _masterpath_default(self):
return os.getcwd()
def _line(self):
"""Return the (x, y) of the straight line described by the sliders."""
x = np.arange(10)
return x, self.gradient * x + self.yintcept
def _replot(self):
self.main.plot_xy(*self._line())
# Both sliders describe the same line, so they share one handler.
def _gradient_changed(self):
self._replot()
def _yintcept_changed(self):
self._replot()
def _plotbutton_fired(self):
self._replot()
def _rangex_changed(self):
self.yval = 1.0 / self.rangex
def _button1_fired(self):
self.changedcount += 1
self.floatval = random.random()
def _floatval_changed(self):
self.stringopt = "Hey, you changed the value!"