-
Notifications
You must be signed in to change notification settings - Fork 7
/
MultiList.py
executable file
·296 lines (230 loc) · 11.9 KB
/
MultiList.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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import sys
if sys.version_info[0] < 3:
from Tkinter import *
else:
from tkinter import *
import General
class Table(object):
def __init__(self, Master, nCol, ColNames, ColWidth, Spacer, Highlight, Font, Color):
self.Master = Master
# Font
self.Font = Font
# Font Color
self.Color = Color
# Scalar
self.nCol = nCol
self.current = None
# Lists
self.ColNames = ColNames
self.ColWidth = ColWidth
self.Spacer = Spacer
self.Highlight = Highlight
# Dictionary
self.Columns = dict()
# Draws the whole Widget
def Draw(self):
self.build_Column()
self.draw_Header()
self.draw_VSB()
self.draw_List()
def Undraw(self):
self.Clear()
for col in self.Columns.keys():
try:
self.Columns[col]['List'].pack_forget()
self.Columns[col]['List'].delete(0, END)
self.Columns[col]['Header'].pack_forget()
self.Columns[col]['Frame'].pack_forget()
self.Columns[col].delete(0,END)
except:
pass
# Builds the columns (Frames)
def build_Column(self):
# Builds the frame columns
for i in range(0, self.nCol):
self.Columns[self.ColNames[i]] = { 'Frame': Frame(self.Master,
width=self.ColWidth[i],
relief=RAISED,
border=1) }
self.Columns[self.ColNames[i]]['Frame'].pack(fill=BOTH, expand=True, side=LEFT)
self.Columns[self.ColNames[i]]['Frame'].pack_propagate(0)
self.Columns[self.ColNames[i]]['Highlight'] = self.Highlight[i]
self.Columns[self.ColNames[i]]['Spacer'] = self.Spacer[i]
# Draws the header
def draw_Header(self):
for i in range(0, self.nCol):
self.Columns[self.ColNames[i]]['Header'] = Label(self.Columns[self.ColNames[i]]['Frame'],
text=self.ColNames[i],
font=self.Font,
width=self.ColWidth[i],
relief=RAISED)
self.Columns[self.ColNames[i]]['Header'].pack(side=TOP)
self.Columns[self.ColNames[i]]['Header'].bind('<Button-1>', lambda event, by=self.ColNames[i]: self.Sort(event, by))
# Draws the header
def draw_VSB(self):
self.vsb = Scrollbar(self.Columns[self.ColNames[self.nCol-1]]['Frame'],
orient='vertical',
command=self.OnVsb)
self.vsb.pack(fill=Y, side=RIGHT)
# Draws the Lists
def draw_List(self):
for i in range(0, self.nCol):
self.Columns[self.ColNames[i]]['List'] = Listbox(self.Columns[self.ColNames[i]]['Frame'],
yscrollcommand=self.vsb.set,
selectmode=SINGLE,
selectborderwidth=0,
selectbackground=self.Color,
selectforeground='white',
highlightthickness=0,
width=self.ColWidth[i],
font=self.Font)
self.Columns[self.ColNames[i]]['List'].pack(fill=Y, expand=True, side=LEFT)
self.Columns[self.ColNames[i]]['List'].bind('<Button-1>',
lambda event, List=self.Columns[self.ColNames[i]]['List']:
self.OnButtonClick(event, List))
# Mac/Windows
self.Columns[self.ColNames[i]]['List'].bind('<MouseWheel>', self.OnListboxMouseWheel)
# Linux MouseWheel Down
self.Columns[self.ColNames[i]]['List'].bind('<Button-4>', self.OnListboxMouseWheel)
# Linux MouseWheel Up
self.Columns[self.ColNames[i]]['List'].bind('<Button-5>', self.OnListboxMouseWheel)
self.Columns[self.ColNames[i]]['StringVar'] = StringVar()
self.Columns[self.ColNames[i]]['StringVar'].set('')
''' ==================================================================================
FUNCTION OnVsb: Permit to Scroll listboxes at the same time
================================================================================== '''
def OnVsb(self, *args):
for col in self.Columns.keys():
self.Columns[col]['List'].yview(*args)
''' ==================================================================================
FUNCTION OnButtonClick: Selects identical index from the other lists
================================================================================== '''
def OnButtonClick(self, event, List):
if List.size() > 0:
Index = List.nearest(event.y)
else:
return
if Index != self.current and Index != '':
if self.current != None:
for col in self.Columns.keys():
if self.Columns[col]['Highlight']:
self.Columns[col]['List'].itemconfig(self.current, bg='white', foreground='black')
for col in self.Columns.keys():
if self.Columns[col]['Highlight']:
self.Columns[col]['List'].itemconfig(Index, bg=self.Color, foreground='white')
self.Columns[col]['StringVar'].set(self.Columns[col]['List'].get(Index)[1:])
self.current = Index
''' ==================================================================================
FUNCTION OnListboxMouseWheel: Scroll the Listboxes based on the mouse wheel event.
================================================================================== '''
def OnListboxMouseWheel(self, event):
# Convert mousewheel motion to scrollbar motion.
if event.num == 4: # Linux encodes wheel as 'buttons' 4 and 5
delta = -1
elif event.num == 5:
delta = 1
else: # Windows & OSX
delta = event.delta
for col in self.Columns:
self.Columns[col]['List'].yview("scroll", delta, "units")
# Return 'break' to prevent the default bindings from
# firing, which would end up scrolling the widget twice.
return "break"
''' ==================================================================================
FUNCTION Clear: Clears all the data in the table
================================================================================== '''
def Clear(self):
for col in self.Columns.keys():
try:
self.Columns[col]['List'].selection_clear(0, END)
except:
continue
for col in self.Columns.keys():
try:
self.Columns[col]['List'].delete(0, END)
except:
continue
self.current = None
''' ==================================================================================
FUNCTION Add: Adds ONE item to listboxes
================================================================================== '''
def Add(self, Item, BGColor):
for i in range(0, self.nCol):
try:
self.Columns[self.ColNames[i]]['List'].insert(END, General.repeat(' ', self.Columns[self.ColNames[i]]['Spacer']) + str(Item[i]))
if BGColor[i] != None:
self.Columns[self.ColNames[i]]['List'].itemconfig(self.Columns[self.ColNames[i]]['List'].size()-1, bg=BGColor[i])
except:
pass
''' ==================================================================================
FUNCTION Add_List: Adds MULTIPLE items to listboxes
================================================================================== '''
def Add_List(self, Items, BGColors):
for n in range(0, len(Items)):
for i in range(0, self.nCol):
self.Columns[self.ColNames[i]]['List'].insert(END, General.repeat(' ', self.Columns[self.ColNames[i]]['Spacer']) + str(Items[n][i]))
if BGColors[n][i] != None:
self.Columns[self.ColNames[i]]['List'].itemconfig(self.Columns[self.ColNames[i]]['List'].size()-1,
bg=BGColors[n][i])
''' ==================================================================================
FUNCTION Delete: Deletes ONE item of the listboxes
================================================================================== '''
def Delete(self, Item, Col):
Found = False
# Does the column exist
for i in range(0, self.nCol):
if self.ColNames[i] == Col:
Found = True
break
if Found:
# Does item exist in column
Index = 0
Found = False
for item in self.Columns[Col]['List'].get(0, END):
if item.lstrip() == str(Item):
Found = True
break
Index += 1
if Found:
# Delete all item at index in each column
for i in range(0, self.nCol):
self.Columns[self.ColNames[i]]['List'].delete(Index)
''' ==================================================================================
FUNCTION Set: Sets ONE item of the listboxes
================================================================================== '''
def Set(self, Item, Col, Value, UptCol):
Found = False
# Does the column exist
for i in range(0, self.nCol):
if self.ColNames[i] == Col:
Found = True
break
if Found:
# Does item exist in column
Index = 0
Found = False
for item in self.Columns[Col]['List'].get(0, END):
#if item.replace(General.repeat(' ',self.Columns[Col]['Spacer']),'') == str(Item):
if item.lstrip() == str(Item):
Found = True
break
Index += 1
if Found:
# Does the update column exist
Found = False
for i in range(0, self.nCol):
if self.ColNames[i] == UptCol:
Found = True
break
if Found:
# Set item value
self.Columns[UptCol]['List'].delete(Index)
try:
self.Columns[UptCol]['List'].insert(Index, General.repeat(' ',self.Columns[UptCol]['Spacer']) + str(Value))
except:
self.Columns[UptCol]['List'].insert(END, General.repeat(' ',self.Columns[UptCol]['Spacer']) + str(Value))
''' ==================================================================================
FUNCTION Sort: Sorts by a column name
================================================================================== '''
def Sort(self, event, by):
return