-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
126 lines (100 loc) · 3.85 KB
/
Copy pathmain.py
File metadata and controls
126 lines (100 loc) · 3.85 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env python3
# Brendan Griffen - January 2019
"""Entry point for the template application.
Layout is a horizontal split: a matplotlib canvas plus its marker controls on
the left, and a tabbed panel of calculation widgets on the right. Add a tab by
writing another ``HasTraits`` class alongside :class:`~firstcalc.FirstCalc` and
adding it to ``right_panel``.
"""
from matplotlib.figure import Figure
from traits.api import Any, Enum, HasTraits, Instance, Range
from traitsui.api import Group, HGroup, HSplit, Item, Tabbed, View
from CommonMPL import MPLFigureEditor
from firstcalc import FirstCalc
class ApplicationMain(HasTraits):
"""Top-level window owning the shared figure and its display settings."""
firstcalc = Instance(FirstCalc)
display = Instance(Figure)
markercolor = Enum("blue", "red", "green", "black", "orange")
markerstyle = Enum("o", "+", ",", "*", "s", "p", "d")
markersize = Range(0, 10, 2)
#: The matplotlib Line2D currently on the axes, or None before first plot.
display_points = Any(None)
left_panel = Group(
Item("display", editor=MPLFigureEditor(), show_label=False, resizable=True),
HGroup(
Item(name="markerstyle", label="Marker"),
Item(name="markersize", label="Size"),
Item(name="markercolor", label="Colour"),
),
)
right_panel = Tabbed(
Item("firstcalc", style="custom", label="First Tab", show_label=False)
)
view = View(
HSplit(left_panel, right_panel),
width=1280,
height=550,
resizable=True,
title="My First Python3 GUI Interface",
)
def _display_default(self):
"""Initialise the display."""
figure = Figure()
axes = figure.add_subplot(111)
axes.set_xlabel("X")
axes.set_ylabel("Y")
axes.set_xlim(0, 1)
axes.set_ylim(0, 1)
# Set matplotlib canvas colour to be white
figure.patch.set_facecolor("w")
return figure
def _firstcalc_default(self):
return FirstCalc(self)
def plot_xy(self, x, y):
"""Draw ``x``/``y`` using the current marker settings.
Reuses the existing line and axes rather than clearing the figure, so
the axis labels survive a replot and the marker controls keep a live
handle on what is drawn.
"""
axes = self.display.axes[0]
if self.display_points is None:
(self.display_points,) = axes.plot(
x,
y,
color=self.markercolor,
marker=self.markerstyle,
markersize=self.markersize,
markeredgewidth=0.0,
linestyle="-",
)
else:
self.display_points.set_data(x, y)
# _display_default pins 0..1 as a placeholder for the empty plot, which
# switches autoscaling off; turn it back on now there is real data.
axes.relim()
axes.autoscale(enable=True)
self.redraw()
def redraw(self):
"""Request a canvas repaint, tolerating a figure with no canvas yet."""
canvas = self.display.canvas
if canvas is not None:
canvas.draw_idle()
def _markercolor_changed(self):
if self.display_points is not None:
self.display_points.set_color(self.markercolor)
self.display_points.set_markeredgecolor(self.markercolor)
self.redraw()
def _markerstyle_changed(self):
if self.display_points is not None:
self.display_points.set_marker(self.markerstyle)
self.redraw()
def _markersize_changed(self):
if self.display_points is not None:
self.display_points.set_markersize(self.markersize)
self.redraw()
def run():
"""Launch the application."""
ApplicationMain().configure_traits()
if __name__ == "__main__":
run()