-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwidgets.py
511 lines (447 loc) · 16.7 KB
/
widgets.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
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
import tkinter as tk
from tkinter import ttk
from pathlib import Path
PATH = str(Path(Path(__file__).parent.resolve()))
class Find(tk.Toplevel):
"""Find whole or partial words within a text widget"""
def __init__(self, master, text_widget):
super().__init__(master)
self.text = text_widget
self.title('Find')
self.transient(master)
self.resizable(False, False)
self.wm_attributes('-topmost', 'true', '-toolwindow', 'true')
self.protocol("WM_DELETE_WINDOW", self.cancel)
self.focus_set()
# create widgets
lbl = ttk.Label(self, text='Find what:')
self.text_find = ttk.Entry(self, width=30, font='consolas 10')
self.btn_next = ttk.Button(self, text='Find Next', width=10, command=self.ask_find_match)
self.whole_word_var = tk.IntVar()
self.whole_word_var.set(0)
check_btn = tk.Checkbutton(
self,
text='Match whole word only',
variable=self.whole_word_var,command=self.change_match_type)
# add widgets to window
lbl.grid(row=0, column=0, padx=(15, 2), pady=15)
self.text_find.grid(row=0, column=1, sticky=tk.EW, padx=5, pady=15)
self.btn_next.grid(row=0, column=2, sticky=tk.EW, padx=(5, 15), pady=15)
self.text_find.focus_set()
check_btn.grid(row=1, column=0, padx=15, pady=(5, 15), columnspan=2, sticky=tk.EW)
# other variables
self.chars = 0
self.term = None
self.start = '1.0'
# configure text widget tags
self.text.tag_configure('found', foreground='black', background='silver')
self.text.tag_configure('found.focus', foreground='white', background='SystemHighlight')
# add additional bindings
self.bind("<Return>", self.ask_find_match)
windowWidth, windowHeight = master.winfo_width(), master.winfo_height()
screenWidth = self.winfo_screenwidth()
screenHeight = self.winfo_screenheight()
xCordinate = int((screenWidth/2) - (windowWidth/2))
yCordinate = int((screenHeight/2) - (windowHeight/2))
self.geometry("+{}+{}".format(xCordinate, yCordinate))
self.mainloop()
def cancel(self):
"""Cancel the request and return control to main window"""
try:
end = self.start
start = self.start + f'-{self.chars}c'
self.text.tag_delete('found', 1.0, tk.END)
self.text.tag_delete('found.focus', 1.0, tk.END)
self.text.tag_add(tk.SEL, start, end)
self.text.mark_set(tk.INSERT, start)
self.text.focus_set()
self.destroy()
except _tkinter.TclError:
#just in case nothing is to find..
self.destroy()
def change_match_type(self):
"""Reset found tags when match type is changed"""
self.term = None
self.chars = None
self.text.tag_remove('found', '1.0', tk.END)
self.text.tag_remove('found.focus', '1.0', tk.END)
def ask_find_match(self, event=None):
"""Check for new searches, and route traffic by search types"""
term = self.text_find.get()
if term == '':
return
if self.term != term:
self.term = term
self.chars = len(term)
self.text.tag_remove('found', '1.0', tk.END)
self.route_match()
self.highlight_next_match()
def route_match(self):
"""Direct to whole or partial match"""
if self.whole_word_var.get():
self.whole_word_matches()
else:
self.partial_word_matches()
def whole_word_matches(self):
"""Locate and tag all whole word matches"""
start = '1.0'
while True:
start = self.text.search(self.term, start, stopindex=tk.END)
if not start:
break
end = start + ' wordend'
found = self.text.get(start + '-1c', end) # whole word includes a space before
if found == ' ' + self.term:
self.text.tag_add('found', start, end)
start = end
def partial_word_matches(self):
"""Locate and tag all partial word matches"""
start = '1.0'
while True:
start = self.text.search(self.term, start, stopindex=tk.END)
if not start:
break
end = start + f'+{self.chars}c'
self.text.tag_add('found', start, end)
start = end
def highlight_next_match(self):
"""Highlight the next matching word"""
self.text.tag_remove('found.focus', '1.0', tk.END) # remove existing tag
try:
start, end = self.text.tag_nextrange('found', self.start, tk.END)
self.text.tag_add('found.focus', start, end)
self.text.mark_set(tk.INSERT, start)
self.text.see(start)
self.start = end
except ValueError:
if self.start != '1.0':
self.start = '1.0'
self.text.see('1.0')
self.highlight_next_match()
class Replace(tk.Toplevel):
"""Find and replace words within a text widget"""
def __init__(self, master, text_widget):
super().__init__(master)
self.text = text_widget
self.title('Find and Replace')
self.transient(master)
self.resizable(False, False)
self.wm_attributes('-topmost', 'true', '-toolwindow', 'true')
self.protocol("WM_DELETE_WINDOW", self.cancel)
self.focus_set()
# create widgets
lbl1 = ttk.Label(self, text='Find what:', anchor=tk.W)
self.text_find = ttk.Entry(self, width=30, font='-size 10')
self.text_find.focus_set()
self.btn_next = ttk.Button(self, text='Find Next', width=12, command=self.ask_find_match)
lbl2 = ttk.Label(self, text='Replace with:', anchor=tk.W)
self.text_replace = ttk.Entry(self, width=30, font='-size 10')
self.btn_replace = ttk.Button(self, text='Replace', width=12, command=self.find_replace_next)
self.btn_replace_all = ttk.Button(self, text='Replace All', width=12, command=self.find_replace_all)
self.whole_word_var = tk.IntVar()
self.whole_word_var.set(0)
check_btn = tk.Checkbutton(
self,
text='Match whole word only',
variable=self.whole_word_var, command=self.change_match_type)
# add widgets to window
lbl1.grid(row=0, column=0, sticky=tk.EW, padx=(15, 2), pady=(15, 0))
lbl2.grid(row=1, column=0, sticky=tk.EW, padx=(15, 2))
self.text_find.grid(row=0, column=1, sticky=tk.EW, padx=5, pady=(15, 2))
self.text_replace.grid(row=1, column=1, sticky=tk.EW, padx=5)
self.btn_next.grid(row=0, column=2, sticky=tk.EW, padx=(5, 15), pady=(15, 2))
self.btn_replace.grid(row=1, column=2, sticky=tk.EW, padx=(5, 15), pady=2)
self.btn_replace_all.grid(row=2, column=2, sticky=tk.EW, padx=(5, 15), pady=(2, 15))
check_btn.grid(row=2, column=0, columnspan=2, sticky=tk.EW, padx=15, pady=(5, 15))
# other variables
self.chars = 0
self.term = None
self.start = '1.0'
# configure text widget tags
self.text.tag_configure('found', foreground='black', background='silver')
self.text.tag_configure('found.focus', foreground='white', background='SystemHighlight')
# add additional bindings
self.bind("<Return>", self.ask_find_match)
windowWidth, windowHeight = master.winfo_width(), master.winfo_height()
screenWidth = self.winfo_screenwidth()
screenHeight = self.winfo_screenheight()
xCordinate = int((screenWidth/2) - (windowWidth/2))
yCordinate = int((screenHeight/2) - (windowHeight/2))
self.geometry("+{}+{}".format(xCordinate, yCordinate))
def cancel(self):
"""Cancel the request and return control to main window"""
try:
end = self.start
start = self.start + f'-{self.chars}c'
self.text.tag_delete('found', 1.0, tk.END)
self.text.tag_delete('found.focus', 1.0, tk.END)
self.text.tag_add(tk.SEL, start, end)
self.text.mark_set(tk.INSERT, start)
self.text.focus_set()
self.destroy()
except _tkinter.TclError:
#raised when there's nothing highlighted!
self.destroy()
def change_match_type(self):
"""Reset found tags when match type is changed"""
self.term = None
self.chars = None
self.text.tag_remove('found', '1.0', tk.END)
self.text.tag_remove('found.focus', '1.0', tk.END)
def ask_find_match(self, event=None):
"""Check for new searches, and route traffic by search types"""
term = self.text_find.get()
if term == '':
return
if self.term != term:
self.term = term
self.chars = len(term)
self.text.tag_remove('found', '1.0', tk.END)
self.route_match()
self.highlight_next_match()
def route_match(self):
"""Direct to whole or partial match"""
if self.whole_word_var.get():
self.whole_word_matches()
else:
self.partial_word_matches()
def whole_word_matches(self):
"""Locate and tag all whole word matches"""
start = '1.0'
while True:
start = self.text.search(self.term, start, stopindex=tk.END)
if not start:
break
end = start + ' wordend'
found = self.text.get(start + '-1c', end) # whole word includes a space before
if found == ' ' + self.term:
self.text.tag_add('found', start, end)
start = end
def partial_word_matches(self):
"""Locate and tag all partial word matches"""
start = '1.0'
while True:
start = self.text.search(self.term, start, stopindex=tk.END)
if not start:
break
end = start + f'+{self.chars}c'
self.text.tag_add('found', start, end)
start = end
def highlight_next_match(self):
"""Highlight the next matching word"""
self.text.tag_remove('found.focus', '1.0', tk.END) # remove existing tag
try:
start, end = self.text.tag_nextrange('found', self.start, tk.END)
self.text.tag_add('found.focus', start, end)
self.text.mark_set(tk.INSERT, start)
self.text.see(start)
self.start = end
except ValueError:
if self.start != '1.0':
self.start = '1.0'
self.text.see('1.0')
self.highlight_next_match()
def find_replace_next(self):
"""Find the next available match and replace it"""
old_term = self.text_find.get()
new_term = self.text_replace.get()
start = self.text.search(old_term, tk.INSERT, tk.END)
try:
self.text.replace(start, start + ' wordend', new_term)
self.highlight_next_match()
except tk.TclError:
return
def find_replace_all(self):
"""Find all matches and replace"""
old_term = self.text_find.get()
new_term = self.text_replace.get()
while True:
start = self.text.search(old_term, '1.0', tk.END)
if not start:
break
self.text.replace(start, start + ' wordend', new_term)
class ToolTip(object):
def __init__(self, widget):
self.widget = widget
self.tipwindow = None
self.id = None
self.x = self.y = 0
self.text = None
def showtip(self, text):
"""Display text in tooltip window"""
self.text = text
if self.tipwindow or not self.text:
return
x, y, cx, cy = self.widget.bbox("insert")
x = x + self.widget.winfo_rootx() + 57
y = y + cy + self.widget.winfo_rooty() + 27
self.tipwindow = tw = tk.Toplevel(self.widget)
tw.wm_overrideredirect(1)
tw.wm_geometry("+%d+%d" % (x, y))
label = tk.Label(tw, text=self.text, justify=tk.LEFT,
fg="#101010",
background="#ffffe0", relief=tk.SOLID, borderwidth=1,
font=("Consolas", "8", "normal"))
label.pack(ipadx=1)
def hidetip(self):
tw = self.tipwindow
self.tipwindow = None
if tw:
tw.destroy()
def create_tool_tip(widget, text):
tool_tip = ToolTip(widget)
def enter(event):
tool_tip.showtip(text)
def leave(event):
tool_tip.hidetip()
widget.bind('<Enter>', enter)
widget.bind('<Leave>', leave)
class AutocompleteEntry(ttk.Entry):
"""
Subclass of tkinter.Entry that features autocompletion.
To enable autocompletion use set_completion_list(list) to define
a list of possible strings to hit.
To cycle through hits use down and up arrow keys.
"""
def set_completion_list(self, completion_list):
self._completion_list = completion_list
self._hits = []
self._hit_index = 0
self.position = 0
self.bind('<KeyRelease>', self.handle_keyrelease)
def autocomplete(self, delta=0):
"""autocomplete the Entry, delta may be 0/1/-1 to cycle through possible hits"""
if delta: # need to delete selection otherwise we would fix the current position
self.delete(self.position, tk.END)
else: # set position to end so selection starts where textentry ended
self.position = len(self.get())
# collect hits
_hits = []
for element in self._completion_list:
if element.startswith(self.get().lower()):
_hits.append(element)
# if we have a new hit list, keep this in mind
if _hits != self._hits:
self._hit_index = 0
self._hits=_hits
# only allow cycling if we are in a known hit list
if _hits == self._hits and self._hits:
self._hit_index = (self._hit_index + delta) % len(self._hits)
# now finally perform the auto completion
if self._hits:
self.delete(0,tk.END)
self.insert(0,self._hits[self._hit_index])
self.select_range(self.position,tk.END)
def handle_keyrelease(self, event):
"""event handler for the keyrelease event on this widget"""
if event.keysym == "BackSpace":
self.delete(self.index(tk.INSERT), tk.END)
self.position = self.index(tk.END)
if event.keysym == "Left":
if self.position < self.index(tk.END): # delete the selection
self.delete(self.position, tk.END)
else:
self.position = self.position-1 # delete one character
self.delete(self.position, tk.END)
if event.keysym == "Right":
self.position = self.index(tk.END) # go to end (no selection)
if event.keysym == "Down":
self.autocomplete(1) # cycle to next hit
if event.keysym == "Up":
self.autocomplete(-1) # cycle to previous hit
# perform normal autocomplete if event is a single key or an umlaut
if len(event.keysym) == 1: # or event.keysym in tkinter_umlauts:
self.autocomplete()
class Notebook(ttk.Notebook):
"""A ttk Notebook with close buttons on each tab"""
__initialized = False
def __init__(self, *args, **kwargs):
if not self.__initialized:
self.__initialize_custom_style()
self.__inititialized = True
kwargs["style"] = "CustomNotebook.TNotebook"
ttk.Notebook.__init__(self, *args, **kwargs)
self._active = None
self.bind("<ButtonPress-1>", self.on_close_press, True)
self.bind("<ButtonRelease-1>", self.on_close_release)
def on_close_press(self, event):
"""Called when the button is pressed over the close button"""
element = self.identify(event.x, event.y)
if "close" in element:
index = self.index("@%d,%d" % (event.x, event.y))
self.state(['pressed'])
self._active = index
return "break"
def on_close_release(self, event):
"""Called when the button is released"""
if not self.instate(['pressed']):
return
element = self.identify(event.x, event.y)
if "close" not in element:
# user moved the mouse off of the close button
return
index = self.index("@%d,%d" % (event.x, event.y))
if self._active == index:
self.forget(index)
self.event_generate("<<NotebookTabClosed>>")
self.state(["!pressed"])
self._active = None
def __initialize_custom_style(self):
style = ttk.Style()
self.images = (
tk.PhotoImage("img_close", file=PATH+'/DATA/icons/close.n.png'),
tk.PhotoImage("img_closeactive", file=PATH+'/DATA/icons/close.h.png'),
tk.PhotoImage("img_closepressed", file=PATH+'/DATA/icons/close.a.png'),
)
style.element_create(
"close",
"image",
"img_close",
("active", "pressed", "!disabled", "img_closepressed"),
("active", "!disabled", "img_closeactive"),
border=8,
sticky='',
)
style.layout(
"CustomNotebook.TNotebook",
[("CustomNotebook.TNotebook.client", {"sticky": "nswe"})],
)
style.layout(
"CustomNotebook.TNotebook.Tab",
[
(
"CustomNotebook.TNotebook.tab",
{
"sticky": "nswe",
"children": [
(
"CustomNotebook.TNotebook.padding",
{
"side": "top",
"sticky": "nswe",
"children": [
(
"CustomNotebook.TNotebook.focus",
{
"side": "top",
"sticky": "nswe",
"children": [
(
"CustomNotebook.TNotebook.label",
{"side": "left", "sticky": ''},
),
(
"CustomNotebook.TNotebook.close",
{"side": "left", "sticky": ''},
),
],
},
)
],
},
)
],
},
)
],
)