-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.py
More file actions
387 lines (297 loc) · 12.6 KB
/
tools.py
File metadata and controls
387 lines (297 loc) · 12.6 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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# -*- coding: utf-8 -*-
"""
project: general purpose
mailto: [giuseppecostanzi@gmail.com]
modify: aestas MMXXI
@author: 1966bc
"""
import tkinter as tk
from tkinter import messagebox
from tkinter import font
from tkinter import ttk
from tkinter.scrolledtext import ScrolledText
class Tools:
def __init__(self,):
super().__init__()
def __str__(self):
return "class: {0}".format((self.__class__.__name__, ))
def set_style(self, theme):
self.style = ttk.Style()
self.style.theme_use(theme)
self.style.configure(".", background=self.get_rgb(240, 240, 237),
font=('TkFixedFont'))
self.style.configure("Product.TEntry",
foreground=self.get_rgb(0, 0, 255),
background=self.get_rgb(255, 255, 255))
self.style.configure("Package.TEntry",
foreground=self.get_rgb(255, 0, 0),
background=self.get_rgb(255, 255, 255))
self.style.configure('W.TFrame', background=self.get_rgb(240, 240, 237))
self.style.configure('W.TButton',
background=self.get_rgb(240, 240, 237),
padding=5,
border=1,
relief=tk.RAISED,
font="TkFixedFont")
self.style.configure('W.TLabel',
background=self.get_rgb(240, 240, 237),
padding=2,
font="TkFixedFont")
self.style.configure('W.TRadiobutton',
background=self.get_rgb(240, 240, 237),
padding=4,
font="TkFixedFont")
self.style.map('W.TCheckbutton',
indicatoron=[('pressed', '#ececec'), ('selected', '#4a6984')])
self.style.configure('W.TCombobox',
background=self.get_rgb(240, 240, 237),
font="TkFixedFont")
self.style.configure('W.TLabelframe',
background=self.get_rgb(240, 240, 237),
relief=tk.GROOVE,
padding=2,
font="TkFixedFont")
self.style.configure('StatusBar.TLabel',
background=self.get_rgb(240, 240, 237),
padding=2,
border=1,
relief=tk.SUNKEN,
font="TkFixedFont")
self.style.map('Treeview', foreground=self.fixed_map('foreground'), background=self.fixed_map('background'))
self.style.configure("Treeview.Heading", background=self.get_rgb(240, 240, 237), font=('TkHeadingFont', 10))
self.style.layout("Treeview", [('Treeview.treearea', {'sticky': 'nswe'})])
self.style.configure("Mandatory.TLabel",
foreground=self.get_rgb(0, 0, 255),
background=self.get_rgb(255, 255, 255))
def get_rgb(self, r, g, b):
"""translates an rgb tuple of int to a tkinter friendly color code"""
return "#%02x%02x%02x" % (r, g, b)
def center_me(self, container):
"""center window on the screen"""
x = (container.winfo_screenwidth() - container.winfo_reqwidth()) / 2
y = (container.winfo_screenheight() - container.winfo_reqheight()) / 2
container.geometry("+%d+%d" % (x, y))
def cols_configure(self, w):
w.columnconfigure(0, weight=0)
w.columnconfigure(1, weight=0)
w.columnconfigure(2, weight=1)
w.rowconfigure(0, weight=0)
w.rowconfigure(1, weight=0)
w.rowconfigure(2, weight=1)
w.rowconfigure(2, pad=8)
def get_init_ui(self, container):
"""All insert,update modules have this same configuration on init_ui.
A Frame, a columnconfigure and a grid method.
So, why rewrite every time?"""
w = ttk.Frame(container, style='W.TFrame')
self.cols_configure(w)
w.grid(row=0, column=0, sticky=tk.N+tk.W+tk.S+tk.E)
return w
def get_label_frame(self, container, text=None, padding=None):
return ttk.LabelFrame(container, style="W.TLabelframe", text=text, padding=padding)
def get_label(self, container, text, textvariable=None, anchor=None, style=None, args=()):
w = ttk.Label(container,
text=text,
textvariable=textvariable,
anchor=anchor,
style='W.TLabel')
if args:
w.grid(row=args[0], column=args[1], sticky=args[2])
else:
w.pack(fill=tk.X, padx=5, pady=5)
return w
def get_spin_box(self, container, text, frm, to, width, var=None, callback=None):
w = self.get_label_frame(container, text=text,)
tk.Spinbox(w,
bg='white',
from_=frm,
to=to,
justify=tk.CENTER,
width=width,
wrap=False,
insertwidth=1,
textvariable=var).pack(anchor=tk.CENTER)
return w
def get_scale(self, container, text, frm, to, width, var=None, callback=None):
w = self.get_label_frame(container, text=text,)
tk.Scale(w,
from_=frm,
to=to,
orient=tk.HORIZONTAL,
variable=var).pack(anchor=tk.N)
return w
def get_radio_buttons(self, container, text, ops, v, callback=None):
w = self.get_label_frame(container, text=text)
for index, text in enumerate(ops):
ttk.Radiobutton(w,
style='W.TRadiobutton',
text=text,
variable=v,
command=callback,
value=index,).pack(anchor=tk.W)
return w
def set_font(self, family, size, weight=None):
if weight is not None:
weight = weight
else:
weight = tk.NORMAL
return font.Font(family=family, size=size, weight=weight)
def get_listbox(self, container, height=None, width=None, color=None):
sb = ttk.Scrollbar(container, orient=tk.VERTICAL)
w = tk.Listbox(container,
relief=tk.GROOVE,
selectmode=tk.BROWSE,
exportselection=0,
height=height,
width=width,
background=color,
font='TkFixedFont',
yscrollcommand=sb.set,)
sb.config(command=w.yview)
w.pack(side=tk.LEFT, fill=tk.BOTH, expand=1, padx=2, pady=2)
sb.pack(fill=tk.Y, expand=1, padx=2, pady=2)
return w
def get_text_box(self, container, height=None, width=None, row=None, col=None):
w = ScrolledText(container,
wrap=tk.WORD,
bg='light yellow',
relief=tk.GROOVE,
height=height,
width=width,
font='TkFixedFont',)
if row is not None:
#print(row,col)
w.grid(row=row, column=1, sticky=tk.W)
else:
w.pack(side=tk.LEFT, fill=tk.BOTH, expand=1)
return w
def on_fields_control(self, container):
msg = "Please fill all fields."
for w in container.winfo_children():
for field in w.winfo_children():
if type(field) in(ttk.Entry, tk.Entry, ttk.Combobox):
if not field.get():
messagebox.showwarning(container.master.title(), msg, parent=container)
field.focus()
return 0
elif type(field) == ttk.Combobox:
if field.get() not in field.cget('values'):
msg = "You can choice only a value of the list."
messagebox.showwarning(container.master.title(), msg, parent=container)
field.focus()
return 0
def get_tree(self, container, cols, size=None, show=None):
#this is a patch because with tkinter version with Tk 8.6.9 the color assignment with tags dosen't work
#https://bugs.python.org/issue36468
#style = ttk.Style()
if size is not None:
self.style.configure("Treeview",
highlightthickness=0,
bd=0,
font=('TkHeadingFont', size)) # Modify the font of the body
else:
pass
headers = []
for col in cols:
headers.append(col[1])
del headers[0]
if show is not None:
w = ttk.Treeview(container, show=show)
else:
w = ttk.Treeview(container,)
w['columns'] = headers
w.tag_configure('is_enable', background='light gray')
for col in cols:
w.heading(col[0], text=col[1], anchor=col[2],)
w.column(col[0], anchor=col[2], stretch=col[3], minwidth=col[4], width=col[5])
sb = ttk.Scrollbar(container)
sb.configure(command=w.yview)
w.configure(yscrollcommand=sb.set)
w.pack(side=tk.LEFT, fill=tk.BOTH, expand=1)
sb.pack(fill=tk.Y, expand=1)
return w
def fixed_map(self, option):
style = ttk.Style()
# Fix for setting text colour for Tkinter 8.6.9
# From: https://core.tcl.tk/tk/info/509cafafae
#
# Returns the style map for 'option' with any styles starting with
# ('!disabled', '!selected', ...) filtered out.
# style.map() returns an empty list for missing options, so this
# should be future-safe.
return [elm for elm in style.map('Treeview', query_opt=option) if
elm[:2] != ('!disabled', '!selected')]
def get_validate_text(self, caller,):
return (caller.register(self.validate_text),
'%i', '%P', )
def get_validate_integer(self, caller):
return (caller.register(self.validate_integer),
'%d', '%i', '%P', '%s', '%S', '%v', '%V', '%W')
def get_validate_float(self, caller):
return (caller.register(self.validate_float),
'%d', '%i', '%P', '%s', '%S', '%v', '%V', '%W')
def limit_chars(self, c, v, *args):
#print(x,args)
if len(v.get()) > c:
v.set(v.get()[:-1])
def validate_text(self, index, value_if_allowed):
try:
str(value_if_allowed)
return True
except ValueError:
return False
def validate_integer(self, action, index, value_if_allowed,
prior_value, text, validation_type,
trigger_type, widget_name):
# action=1 -> insert
if action == '1':
if text in '0123456789':
try:
int(value_if_allowed)
return True
except ValueError:
return False
else:
return False
else:
return True
def validate_float(self, action, index, value_if_allowed,
prior_value, text, validation_type,
trigger_type, widget_name):
# action=1 -> insert
if action == "1":
if text in '0123456789.-+':
try:
float(value_if_allowed)
return True
except ValueError:
return False
else:
return False
else:
return True
def on_to_assign(self, caller, evt=None):
msg = "To do!"
messagebox.showwarning(self.title, msg, )
def get_widget_attributes(self, container):
all_widgets = container.winfo_children()
for widg in all_widgets:
print('\nWidget Name: {}'.format(widg.winfo_class()))
keys = widg.keys()
for key in keys:
print("Attribute: {:<20}".format(key), end=' ')
value = widg[key]
vtype = type(value)
print('Value: {:<30} Type: {}'.format(value, str(vtype)))
def get_widgets(self, container):
all_widgets = container.winfo_children()
for widg in all_widgets:
print(widg)
print('\nWidget Name: {}'.format(widg.winfo_class()))
#keys = widg.keys()
def main():
foo = Tools()
print(foo)
input('end')
if __name__ == "__main__":
main()