-
Notifications
You must be signed in to change notification settings - Fork 2
/
tkSliderWidget.py
144 lines (127 loc) · 5.14 KB
/
tkSliderWidget.py
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
#https://raw.githubusercontent.com/MenxLi/tkSliderWidget/master/tkSliderWidget.py
from tkinter import *
from tkinter.ttk import *
class Slider(Frame):
LINE_COLOR = "#476b6b"
LINE_WIDTH = 3
BAR_COLOR_INNER = "#5c8a8a"
BAR_COLOR_OUTTER = "#c2d6d6"
BAR_RADIUS = 7
BAR_RADIUS_INNER = BAR_RADIUS-3
DIGIT_PRECISION = '.2f' # for showing in the canvas
def __init__(self, master, width = 400, height = 80, min_val = 0, max_val = 1, init_lis = None, show_value = True):
Frame.__init__(self, master, height = height, width = width)
self.master = master
if init_lis == None:
init_lis = [min_val]
self.init_lis = init_lis
self.max_val = max_val
self.min_val = min_val
self.show_value = show_value
self.H = height
self.W = width
self.canv_H = self.H
self.canv_W = self.W
if not show_value:
self.slider_y = self.canv_H/2 # y pos of the slider
else:
self.slider_y = self.canv_H*2/5
self.slider_x = Slider.BAR_RADIUS # x pos of the slider (left side)
self.bars = []
self.selected_idx = None # current selection bar index
for value in self.init_lis:
pos = (value-min_val)/(max_val-min_val)
ids = []
bar = {"Pos":pos, "Ids":ids, "Value":value}
self.bars.append(bar)
self.canv = Canvas(self, height = self.canv_H, width = self.canv_W)
self.canv.pack()
self.canv.bind("<Motion>", self._mouseMotion)
self.canv.bind("<B1-Motion>", self._moveBar)
self.__addTrack(self.slider_x, self.slider_y, self.canv_W-self.slider_x, self.slider_y)
for bar in self.bars:
bar["Ids"] = self.__addBar(bar["Pos"])
def state(self,indi,FUN):
self.canv.configure(state=indi)
if indi==DISABLED:
self.canv.unbind("<Motion>")
self.canv.unbind("<B1-Motion>")
self.canv.unbind("<Leave>")
if indi==NORMAL:
self.canv.bind("<Motion>", self._mouseMotion)
self.canv.bind("<B1-Motion>", self._moveBar)
self.canv.bind("<Leave>",FUN)
def getValues(self):
values = [bar["Value"] for bar in self.bars]
return sorted(values)
def _mouseMotion(self, event):
x = event.x; y = event.y
selection = self.__checkSelection(x,y)
if selection[0]:
self.canv.config(cursor = "hand2")
self.selected_idx = selection[1]
else:
self.canv.config(cursor = "")
self.selected_idx = None
def _moveBar(self, event):
x = event.x; y = event.y
if self.selected_idx == None:
return False
pos = self.__calcPos(x)
idx = self.selected_idx
self.__moveBar(idx,pos)
def __addTrack(self, startx, starty, endx, endy):
id1 = self.canv.create_line(startx, starty, endx, endy, fill = Slider.LINE_COLOR, width = Slider.LINE_WIDTH)
return id
def __addBar(self, pos):
"""@ pos: position of the bar, ranged from (0,1)"""
if pos <0 or pos >1:
raise Exception("Pos error - Pos: "+str(pos))
R = Slider.BAR_RADIUS
r = Slider.BAR_RADIUS_INNER
L = self.canv_W - 2*self.slider_x
y = self.slider_y
x = self.slider_x+pos*L
id_outer = self.canv.create_oval(x-R,y-R,x+R,y+R, fill = Slider.BAR_COLOR_OUTTER, width = 2, outline = "")
id_inner = self.canv.create_oval(x-r,y-r,x+r,y+r, fill = Slider.BAR_COLOR_INNER, outline = "")
if self.show_value:
y_value = y+Slider.BAR_RADIUS+8
value = pos*(self.max_val - self.min_val)+self.min_val
id_value = self.canv.create_text(x,y_value,font=('Purisa',8), text = format(value, Slider.DIGIT_PRECISION))
return [id_outer, id_inner, id_value]
else:
return [id_outer, id_inner]
def __moveBar(self, idx, pos):
ids = self.bars[idx]["Ids"]
for id in ids:
self.canv.delete(id)
self.bars[idx]["Ids"] = self.__addBar(pos)
self.bars[idx]["Pos"] = pos
self.bars[idx]["Value"] = pos*(self.max_val - self.min_val)+self.min_val
def __calcPos(self, x):
"""calculate position from x coordinate"""
pos = (x - self.slider_x)/(self.canv_W-2*self.slider_x)
if pos<0:
return 0
elif pos>1:
return 1
else:
return pos
def __getValue(self, idx):
"""#######Not used function#####"""
bar = self.bars[idx]
ids = bar["Ids"]
x = self.canv.coords(ids[0])[0] + Slider.BAR_RADIUS
pos = self.__calcPos(x)
return pos*(self.max_val - self.min_val)+self.min_val
def __checkSelection(self, x, y):
"""
To check if the position is inside the bounding rectangle of a Bar
Return [True, bar_index] or [False, None]
"""
for idx in range(len(self.bars)):
id = self.bars[idx]["Ids"][0]
bbox = self.canv.bbox(id)
if bbox[0] < x and bbox[2] > x and bbox[1] < y and bbox[3] > y:
return [True, idx]
return [False, None]