forked from slightlynybbled/tk_tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisual.py
More file actions
268 lines (211 loc) · 8.84 KB
/
visual.py
File metadata and controls
268 lines (211 loc) · 8.84 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import tkinter as tk
import cmath
import os
class Dial(tk.Frame):
"""
Base class for all dials and dial-like widgets
"""
def __init__(self, parent, size=100, **options):
tk.Frame.__init__(self, parent, padx=3, pady=3, borderwidth=2, **options)
self.size = size
def to_absolute(self, x, y):
"""
Converts coordinates provided with reference to the center of the canvas (0, 0)
to absolute coordinates which are used by the canvas object in which (0, 0) is
located in the top left of the object.
:param x: x value in pixels
:param y: x value in pixels
:return:
"""
return x + self.size/2, y + self.size/2
class Compass(Dial):
"""
Displays a compass typically seen on a map
"""
def __init__(self, parent, size=100, **options):
super().__init__(parent, size=size, **options)
raise NotImplementedError()
# todo: import an image, place the image on the canvas, then place an arrow on top of the image
class RotaryScale(Dial):
"""
Shows a rotary scale, much like a speedometer.
"""
def __init__(self, parent, max_value=100.0, size=100, image_path=None, **options):
"""
Initializes the RotaryScale object
:param parent: tkinter parent frame
:param max_value: the value corresponding to the maximum value on the scale
:param size: the size in pixels
:param options: the frame options
"""
super().__init__(parent, size=size, **options)
self.max_value = float(max_value)
self.size = size
self.canvas = tk.Canvas(self, width=self.size, height=self.size)
self.canvas.grid()
this_path = os.path.abspath(os.path.dirname(__file__))
img_path = os.path.join(this_path, 'img/rotary-gauge-bg.png')
self.image = tk.PhotoImage(file=img_path)
self.image = self.image.subsample(int(1000/self.size), int(1000/self.size))
initial_value = 0.0
self.set_value(initial_value)
def set_value(self, number: float):
"""
Sets the value of the graphic
:param number: the number (must be between 0 and 'max_range' or the scale will peg the limits
:return: None
"""
self.canvas.delete('all')
self.canvas.create_image(0, 0, image=self.image, anchor='nw')
number = number if number <= self.max_value else self.max_value
number = 0.0 if number < 0.0 else number
radius = 0.9 * self.size/2.0
angle_in_radians = (2.0 * cmath.pi / 3.0) + number / self.max_value * (5.0 * cmath.pi / 3.0)
center = cmath.rect(0, 0)
outer = cmath.rect(radius, angle_in_radians)
self.canvas.create_line(
*self.to_absolute(center.real, center.imag),
*self.to_absolute(outer.real, outer.imag),
width=5,
fill='blue'
)
def draw_background(self, divisions=10):
"""
Draws the background of the dial
:param divisions: the number of divisions between 'ticks' shown on the dial
:return:
"""
self.canvas.create_arc(2, 2, self.size-2, self.size-2, style=tk.PIESLICE, start=-60, extent=30, fill='red')
self.canvas.create_arc(2, 2, self.size-2, self.size-2, style=tk.PIESLICE, start=-30, extent=60, fill='yellow')
self.canvas.create_arc(2, 2, self.size-2, self.size-2, style=tk.PIESLICE, start=30, extent=210, fill='green')
# find the distance between the center and the inner tick radius
inner_tick_radius = int(self.size * 0.4)
outer_tick_radius = int(self.size * 0.5)
for tick in range(divisions):
angle_in_radians = (2.0 * cmath.pi / 3.0) + tick/divisions * (5.0 * cmath.pi / 3.0)
inner_point = cmath.rect(inner_tick_radius, angle_in_radians)
outer_point = cmath.rect(outer_tick_radius, angle_in_radians)
self.canvas.create_line(
*self.to_absolute(inner_point.real, inner_point.imag),
*self.to_absolute(outer_point.real, outer_point.imag),
width=1
)
class Graph(tk.Frame):
"""
Tkinter native graph (pretty basic, but doesn't require heavy install)
Notes: the core of this object was creating using the
basic structure found at: https://gist.github.com/ajbennieston/3072649
"""
def __init__(self, parent, x_min, x_max, y_min, y_max, x_scale, y_scale, **options):
"""
Initializes the graph object.
:param parent: the parent frame
:param x_min: the x minimum
:param x_max: the x maximum
:param y_min: the y minimum
:param y_max: the y maximum
:param x_scale: the 'tick' on the x-axis
:param y_scale: the 'tick' on the y-axis
:param options: additional valid tkinter.canvas options
"""
tk.Frame.__init__(self, parent)
self.canvas = tk.Canvas(self, **options)
self.canvas.grid(row=0, column=0)
self.w = float(self.canvas.config('width')[4])
self.h = float(self.canvas.config('height')[4])
self.x_min = x_min
self.x_max = x_max
self.x_scale = x_scale
self.y_min = y_min
self.y_max = y_max
self.y_scale = y_scale
self.px_x = (self.w - 100) / ((x_max - x_min) / x_scale)
self.px_y = (self.h - 100) / ((y_max - y_min) / y_scale)
self.draw_axes()
def draw_axes(self):
"""
Removes all existing series and re-draws the axes
:return: None
"""
self.canvas.delete('all')
rect = 50, 50, self.w - 50, self.h - 50
self.canvas.create_rectangle(rect, outline="black")
for x in self.frange(0, self.x_max - self.x_min + 1, self.x_scale):
x_step = (self.px_x * x) / self.x_scale
coord = 50 + x_step, self.h - 50, 50 + x_step, self.h - 45
self.canvas.create_line(coord, fill="black")
coord = 50 + x_step, self.h - 40
self.canvas.create_text(coord, fill="black", text=str(self.x_min + x))
for y in self.frange(0, self.y_max - self.y_min + 1, self.y_scale):
y_step = (self.px_y * y) / self.y_scale
coord = 45, 50 + y_step, 50, 50 + y_step
self.canvas.create_line(coord, fill="black")
coord = 35, 50 + y_step
self.canvas.create_text(coord, fill="black", text=str(self.y_max - y))
def plot_point(self, x, y, visible=True, color='black', size=5):
"""
Places a single point on the
:param x:
:param y:
:param visible: True if the individual point should be visible
:param color: the color of the point
:param size: the point size in pixels
:return: The absolute coordinates as a tuple
"""
xp = (self.px_x * (x - self.x_min)) / self.x_scale
yp = (self.px_y * (self.y_max - y)) / self.y_scale
coord = 50 + xp, 50 + yp
if visible:
# divide down to an appropriate size
size = int(size/2) if int(size/2) > 1 else 1
x, y = coord
self.canvas.create_oval(
x-size, y-size,
x+size, y+size,
fill=color
)
return coord
def plot_line(self, points, color='black', point_visibility=False):
"""
Plot a line of points
:param points: a list of tuples, each tuple containing an (x, y) point
:param color: the color of the line
:param point_visibility: True if the points should be individually visible
:return: None
"""
last_point = ()
for point in points:
this_point = self.plot_point(point[0], point[1], color=color, visible=point_visibility)
if last_point:
self.canvas.create_line(last_point + this_point, fill=color)
last_point = this_point
# print last_point
@staticmethod
def frange(start, stop, step, digits_to_round=3):
"""
Works like range for doubles
:param start: starting value
:param stop: ending value
:param step: the increment
:param digits_to_round: the digits to which to
round (makes floating-point numbers much easier
to work with)
:return: generator
"""
while start < stop:
yield round(start, digits_to_round)
start += step
if __name__ == '__main__':
root = tk.Tk()
p = RotaryScale(root, max_value=20.0)
p.grid(row=0, column=0)
increment = 1.0
value = 0.0
def inc():
global value
value += increment
p.set_value(value)
print(value)
zero_btn = tk.Button(root, text='increment by {}'.format(increment), command=inc)
zero_btn.grid(row=1, column=0)
root.mainloop()