-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathGeometricalFrame.py
807 lines (703 loc) · 26.2 KB
/
GeometricalFrame.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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
from Tkinter import *
from tkFont import Font
from math import *
from tkFont import Font
from math import *
from tkSimpleDialog import *
import tkMessageBox
import tkFileDialog
import re
import ttk
import json
CR = '\n'
# Bugfix 25.02.2019 - on MacOS with MOJAVE there is a problem with Tkinter and
# displaying some widgetes (like buttons). Text is not visible on button (Label)
# Fix: import ttk and all Buttons are ttk.Button, ttk.Checkbutton ,....
#
# style is an easy approach to set all Button with same style
#
# ttk.Radiobuttons: they are declared inside sub python modules :-( (like contourArc.py)
#
ttkStyle = ttk.Style()
ttkStyle.map(
"C.TButton",
foreground=[('pressed', 'blue'), ('active', 'blue')],
background=[('pressed', '!disabled', 'black'), ('active', 'white')])
ttkStyle.map(
"C.Tttk.Radiobutton",
foreground=[('pressed', 'green'), ('active', 'red')],
background=[('pressed', '!disabled', 'black'), ('active', 'white')])
class GeometricalFrame(Frame):
def __init__(self, app, master, frame, title, winGeometry="900x750"):
Frame.__init__(self)
self.app = app
self.parentFrame = frame
self.title = title
self.master = master
self.master.geometry(winGeometry)
self.app.master.title(title)
self.dicMaterials = {} # complete material json list
self.dicSelectedMaterial = {} # current selected material
self.dicSystemConfig = {} # loaded system configuration parameters
self.frmButtonsIndividualContent = Frame(
self.parentFrame,
highlightbackground="darkgray",
highlightcolor="darkgray",
highlightthickness=1)
# format [notuse/use (0/1), GCODE, ....]
#
# deprecated in future
self._standardGCodeSeq = {
"HOMEING": [
"G00 Z{0:08.3f} F{1:05.1f} {2}",
"G00 X{0:08.3f} Y{1:08.3f} F{2:05.1f} {3}"
],
"SPINDLE_CW": ["M3 S{0:08.4f} {1}"],
"SPINDLE_CCW": ["M4 S{0:08.4f} {1}"],
"TOOL": ["T{0:03d} {1}M6 {1}"],
"PREAMBLE": ["G90 G64 G17 G40 G49 {0}"],
"POSTAMBLE": ["G00 Z10 F100 M2 {0}"],
"ZAXIS": [3.0, 10.0],
"TRAVEL_SPEEDXYZ": ["500", "500", "400"], #[X,Y,Z]
}
self.__loadSystemConfig()
self.__loadMillingParameterFile()
self.updateDefaultFields()
def destroy(self):
self.frmImage.destroy()
self.frmStandardContent.destroy()
self.frmButtonsIndividualContent.destroy()
self.frmButtons.destroy()
pass
def show(self,
showImage=True,
showStandardContent=True,
showStandartButton=True):
print "Show"
#
if showImage:
self.__frmImage()
#
if showStandardContent:
self.__frmStandardContent()
#
self._frmIndividualContent()
#
if showStandartButton:
self.__buttonBox()
self.setBtnStates(state=NORMAL)
pass
def __frmImage(self):
print "__frmImage"
self.frmImage = Frame(
self.parentFrame,
highlightbackground="darkgray",
highlightcolor="darkgray",
highlightthickness=1)
self.frmImage.grid(row=0, column=0, rowspan=5)
self.frmImage.pack(expand=True)
pass
def __loadSystemConfig(self):
print("load system json config file")
with open("config.json", "r") as read_file:
self.dicSystemConfig = json.load(read_file)
def updateDefaultFields(self):
'''
set default values from config file
'''
if bool(self.dicSystemConfig):
self._standardGCodeSeq["PREAMBLE"][0] = self.dicSystemConfig[
"Preamble"]
self._standardGCodeSeq["POSTAMBLE"][0] = self.dicSystemConfig[
"Postamble"]
self._standardGCodeSeq["ZAXIS"][0] = self.dicSystemConfig["StartZ"]
self._standardGCodeSeq["ZAXIS"][1] = self.dicSystemConfig[
"SafetyZ"]
self._standardGCodeSeq["TRAVEL_SPEEDXYZ"] = self.dicSystemConfig[
"TravelSpeedXYZ"]
else:
print("no system config file available")
pass
def __loadMillingParameterFile(self):
'''
load json file and save content into self.__millingparameters
'''
print("load JSON Milling parameter file....")
with open("millingparameters.json", "r") as read_file:
self.dicMaterials = json.load(read_file)
#
# insert an unique id for every materials
uid = 0
for (cat,v) in self.dicMaterials.items():
for mat in self.dicMaterials[cat]:
mat['uid'] = uid
uid += 1
#print("--------------------------")
#print(self.dicMaterials)
#print("--------------------------")
def setMaterialDict(self, value):
'''
set internal material dict (dicSelectedMaterial).
'value' is a string representing the text inside material combobox
'''
self.dicSelectedMaterial = self.getMaterialData(value)
def getMaterialData(self, value):
'''
split the combobox string into "Main-Key (e.g. Metal)" and Sub-Key "Material" (e.g. Alu)
Return the compete substring dict "
'''
sp = value.split(',')
# [0] Major Key (e.g Plastic)
# [1] Material (e.g. Polystorol)
# [2] Tool
# [3] ToolDia
# [4] uid (e.g. 11) => id is a "hidden" field, generated during loading json file
return self.getMaterialDataDict(sp[0], sp[1], sp[4])
def upateMaterialFields(self, value):
print("def upateMaterialFields(self, value): {}".format(value))
value = self.material.current()
mat = self.getOptionMenuMillingData()[value]
print(">>Current: {}, {}".format(value, mat))
self.setMaterialDict(mat)
print(self.dicSelectedMaterial)
if "ToolID" in self.dicSelectedMaterial:
self.currentToolID.set(self.dicSelectedMaterial["ToolID"])
self.currentSpindleRPM.set(self.dicSelectedMaterial["Spindel-RPM"])
self.tooldia.set(self.dicSelectedMaterial["Tool dia"])
self.speed_XY_G02G03.set(
self.dicSelectedMaterial["Feed rate mm/min"])
self.speed_Z_G01.set(
self.dicSelectedMaterial["Infeed rate mm/min"])
def getOptionMenuMillingData(self):
'''
create a sorted array of all defined materials
Name of material: (Category) material => Plastic,Polystorol; Wood,beech plywood; ...
'''
data = []
for (k, v) in self.dicMaterials.items():
for list in self.dicMaterials[k]:
s = "{0},{1},({2}mm,{3}mm), {4}".format(k, list["Material"],
list["Tool"], list["Tool dia"],list["uid"])
data.append(s)
return sorted(data)
def getMaterialDataDict(self, category, material, uid):
'''
Return a dictionary for this category and material.
Is used to prefill entry widges like speed, tool dia, ...
Example:
"Material" : "PMMA 4 mm (Plexiglas)",
"Tool" : "Single cutter",
"ToolID" : 12,
"Tool dia" : 2.0,
"Feed rate 1" : 25,
"Feed rate 2" : 1500,
"Infeed rate" : 0.4,
"Spindel-RPM" : 20000,
"Info" : "Einschneid Fraeser",
"uid" : 18
'''
for cat in self.dicMaterials[category]:
print(cat)
if cat["Material"] == material:
if str(cat["uid"]) == uid.strip():
print("found (({}) {},{})").format(uid, cat, material)
return cat
return {}
def __frmStandardContent(self,
showPreamble=True,
showPostamble=True,
showSpindleAndTool=True):
print "__frmStandardContent"
self.frmStandardContent = Frame(
self.parentFrame,
highlightbackground="darkgray",
highlightcolor="darkgray",
highlightthickness=1)
row = 0
if showPreamble:
self._preamble = StringVar()
txt = self._standardGCodeSeq["PREAMBLE"][0].format(CR)
self._preamble.set(txt)
Label(
self.frmStandardContent, text="PreGCode").grid(
row=row, column=0, sticky=W)
FloatEntry(
self.frmStandardContent,
width=70,
mandatory=False,
textvariable=self._preamble).grid(
row=row, column=1, columnspan=6, sticky=W)
if showPostamble:
row += 1
self._postamble = StringVar()
txt = self._standardGCodeSeq["POSTAMBLE"][0].format(CR)
self._postamble.set(txt)
Label(
self.frmStandardContent, text="PostGCode").grid(
row=row, column=0, sticky=W)
FloatEntry(
self.frmStandardContent,
width=70,
mandatory=False,
textvariable=self._postamble).grid(
row=row, column=1, columnspan=6, sticky=W)
# bugfix / requirement #10
if showSpindleAndTool:
row += 1
# New in V0.12.5 -------------------------------
choices = self.getOptionMenuMillingData()
self.selectedMaterial = StringVar()
self.selectedMaterial.set(choices[0])
matWidth = len(max(choices, key=len)) - 20
Label(
self.frmStandardContent, text='Material').grid(
row=row, column=0, sticky=W)
self.material = ttk.Combobox(
self.frmStandardContent,
textvariable=self.selectedMaterial,
values=choices)
self.material.configure(width=matWidth)
print(">>Current: {}".format(self.material.current()))
self.material.bind("<<ComboboxSelected>>",
self.upateMaterialFields)
self.material.config(width=matWidth)
self.material.grid(row=row, column=1, columnspan=2, sticky=W)
#-----------------------------------------------
self.currentSpindleRPM = StringVar(value="")
self.currentToolID = StringVar(value="")
self._spindleCCW = StringVar(value="CW")
Label(
self.frmStandardContent, text="ToolID").grid(
row=row, column=3, sticky=W)
IntEntry(
self.frmStandardContent,
width=10,
mandatory=False,
textvariable=self.currentToolID).grid(
row=row, column=4, sticky=W)
Label(
self.frmStandardContent, text="Spindle Speed").grid(
row=row, column=5, sticky=W)
IntEntry(
self.frmStandardContent,
width=10,
mandatory=False,
textvariable=self.currentSpindleRPM).grid(
row=row, column=6, sticky=W)
# workaround MacOS, Mohjave with TKinter-Buttons
#ttk.Checkbutton(self.frmStandardContent, text="Spindle CCW",
# var=self._spindleCCW, onvalue="CCW", offvalue="CW").grid(
# row=row, column=6, sticky=W
# )
self.frmStandardContent.pack(expand=True, fill=BOTH)
pass
def _frmIndividualContent(self):
#override in subcluss
pass
def __buttonBox(self):
print "__buttonBox"
self.frmButtons = Frame(
self.parentFrame,
highlightbackground="darkgray",
highlightcolor="darkgray",
highlightthickness=1)
# bugfix 25.02.2019, change from "To AXIS" to "To Console"
self.btnAxis = ttk.Button(
self.frmButtons,
text="To Console",
width=10,
command=self.copyConsole,
state=DISABLED,
style="C.TButton")
self.btnAxis.grid(row=0, column=0)
self.btnClip = ttk.Button(
self.frmButtons,
text="To Clipboard",
width=10,
command=self.copyClipboard,
state=DISABLED,
style="C.TButton")
self.btnClip.grid(row=0, column=1)
self.btnSave = ttk.Button(
self.frmButtons,
text="To File",
width=10,
command=self.saveFile,
state=DISABLED,
style="C.TButton")
self.btnSave.grid(row=0, column=2)
self.btnGCode = ttk.Button(
self.frmButtons,
text="gen. GCode",
width=10,
command=self.showGCode,
state=NORMAL,
style="C.TButton")
self.btnGCode.grid(row=0, column=3)
#self.btnGCode = ttk.Button(self.frmButtons, text="Material", width=10,
# command=self.showMaterial, state=DISABLED, style="C.TButton")
#self.btnGCode.grid(
# row=0, column=4
# )
self.btnCancel = ttk.Button(
self.frmButtons,
text="Cancel",
width=10,
command=self.cancel,
state=NORMAL,
style="C.TButton")
self.btnCancel.grid(row=0, column=4)
self.frmButtons.pack(expand=True, fill=BOTH)
def generateGCode(self):
# override from subclass
pass
def getGCode_Homeing(self, x=0, y=0, z=10, fxy=100, fz=100):
gc = "(HOMEING)" + CR
gc += self._standardGCodeSeq["HOMEING"][0].format(z, fz, CR)
gc += self._standardGCodeSeq["HOMEING"][1].format(x, y, fxy, CR)
return gc
def getGCode_SpindleAndTool(self, additional=""):
temp = "(Tool handling)" + CR
#------- Tool handling -----------
if self.currentToolID.get() != "":
t = int(self.currentToolID.get())
if t < 0:
t = 0
msg = "{0} {1:5.2f}mm".format(self.dicSelectedMaterial["Tool"],
self.dicSelectedMaterial["Tool dia"])
temp += "(MSG, change tool to {0} {1}".format(msg, CR)
temp += "T{0:03d} M6 {1}".format(t, CR)
temp += "(Spindel control)" + CR
#------- Spindle control ---------
if self.currentSpindleRPM.get() != "":
s = int(self.currentSpindleRPM.get())
sdir = self._spindleCCW.get()
if s < 0:
s = 0
if sdir == "CW":
temp += "M3 S{0:04d} {1}".format(s, CR)
else:
temp += "M4 S{0:04d} {1}".format(s, CR)
if s == 0:
temp += "M5" + CR
if additional != "":
temp += "(additional)" + CR
temp += additional + CR
return temp + CR
def getGCode_Preamble(self, additional=""):
temp = ""
# Preamble
temp += CR + "(set general preamble)" + CR
temp += self._preamble.get() + CR
if (additional != ""):
temp += "(additional)" + CR
temp += additional + CR
temp += self.getGCode_SpindleAndTool()
return temp
def getGCode_Postamble(self, additional=""):
temp = ""
# Preamble
temp += CR + "(set general postamble)" + CR
temp += self._postamble.get() + CR
if (additional != ""):
temp += "(additional)" + CR
temp += additional + CR
return temp
def getGCodeCutterComp(self, compensation="G40", toolDia=0.0, x=0.0,
y=0.0):
'''
return a GCode for given cutter compensation
This cutter compensation do not use tool table adjustment for
tool diameters.
if toolDia is not set (0.0) than preset tool diameter is used
# if cutter compensation is used please remember:
# G41 is LEFT from path
# G42 is RIGHT from path
#
# if our start position is at 3-clock and G41 is used, tool is inside
# arc (circle), because we should start LEFT from path.
#
# if G42 is used, tool is outside of arc (circle) (RIGHT)
#
# this behaviour depends on general contour direction (CW or CCW)
# CW => above behaviour
# CCW => G41 is RIGHT, G42 is LEFT
'''
gc = ""
#if toolDia == 0.0:
# compensation = "G40"
gc += "(-- cutter compensation --)" + CR
if (compensation == "G41"):
if toolDia == 0.0:
gc += "G41 {1}".format(CR)
else:
gc += "G41.1 D{0:05.2f} X{2:08.3f} Y{3:08.3f}{1}".format(
toolDia, CR, x, y)
#gc += "G41.1 D{0:05.2f}{1}".format(toolDia, CR)
elif (compensation == "G42"):
if toolDia == 0.0:
gc += "G42 {1}".format(CR)
else:
gc += "G42.1 D{0:05.2f} X{2:08.3f} Y{3:08.3f}{1}".format(
toolDia, CR, x, y)
#gc += "G42.1 D{0:05.2f}{1}".format(toolDia, CR)
else: # G40
gc += "G40 {0}".format(CR)
return gc
def copyClipboard(self, event=None):
print "copyClipboard"
gc = self.getGCode()
if gc is None:
return None
self.app.clipboard_clear()
self.app.clipboard_append(gc)
pass
def saveFile(self, event=None):
gc = self.getGCode()
if gc is None:
return None
fname = tkFileDialog.asksaveasfilename(
initialdir="./",
title="Save file",
defaultextension="*.ngc",
filetypes=(("Axis ", "*.ngc"), ("Gcode ", "*.gcode"), ("all files",
"*.*")))
if (fname == None):
# cancle button
return None
print("Save gcode to '{}'".format(fname))
f = open(fname, "w")
f.write(gc)
f.close()
pass
def copyConsole(self, event=None):
print("---------------- copyConsole -----------------")
gc = self.getGCode()
if gc is None:
return None
sys.stdout.write(self.getGCode())
#self.quit()
def showGCode(self, event=None):
gc = self.getGCode()
if gc is None:
return None
d = GCodeDialog(self.app, title="generated GCode")
d.init(gc)
d.update(gc)
pass
def showMaterial(self, event=None):
data = {}
d = EntryGridDialog(self.app, title="MaterialParameters")
d.init(data)
d.update(data)
pass
def setBtnStates(self, state):
self.btnClip.config(state=state)
self.btnSave.config(state=state)
self.btnAxis.config(state=state, default=ACTIVE)
self.btnGCode.config(state=state)
def defaultUserValidation(self):
pre = self._preamble.get()
post = self._postamble.get()
try:
spindle = int(self.currentSpindleRPM.get())
except ValueError:
spindle = -1
try:
toolID = int(self.currentToolID.get())
except ValueError:
toolID = -1
if pre == "":
self.MessageBox(
state="WARN",
title="Warn",
text="Are you shure? There is no preamble gcode available")
return False
if post == "":
self.MessageBox(
state="WARN",
title="Warn",
text="Are you shure? There is no postamble gcode available")
return False
if (spindle < -1 or toolID < -1):
self.MessageBox(
state="INFO",
title="INFO",
text="You set no tool id and/or spindel control")
return True
return True
def userInputValidation(self):
# override in subclass
'''
This function is called from getGCode() and validate all important
input fields in the current dialog.
Implementation should be done inside subclass
This method should return True or False
True if everything is ok
False something is wrong - no GCode generation
'''
pass
def getGCode(self):
if self.defaultUserValidation() == False:
return None
if self.userInputValidation() == False:
return None
gc = "%"
#
# removed because some trouble with GCode-Interpreters (BK-13.12.2018)
gc += '''
; (--------------------------)
; ( __ )
; ( _(\ |@@| )
; ( (__/\__ \--/ __ )
; ( \___|----| | __ )
; ( \ }{ /\ )_ / _\ )
; ( /\__/\ \__O (__ )
; ( (--/\--) \__/ )
; ( _)( )(_ )
; ( `---''---` )
; ( (c) by LunaX 2018 )
; (--------------------------)
'''
gc += CR
gc += self.generateGCode()
gc += "%" + CR
return gc
#------ EXIT --------------------------
def cancel(self, event=None):
print "cancel"
#self.destroy()
self.master.quit()
pass
def MessageBox(self, state="INFO", title="", text=""):
if state == "INFO":
tkMessageBox.showinfo(title, text)
elif state == "WARN":
tkMessageBox.showinfo(title, text)
elif state == "ERROR":
tkMessageBox.showerror(title, text)
else:
tkMessageBox.showtitle("!!!unknown - State !!!", text)
def getDepthSteps(self, total, step):
'''
calculate how many depth steps we need to mill to total depth.
Return two values:
1. Value = numberOfWindings
2. Value = rest depth
'''
r = round((total % step), 3)
w = int(abs(total / step))
return w, r
class ToolTip:
'''
It creates a tooltip for a given widget as the mouse goes on it.
see:
http://stackoverflow.com/questions/3221956/
what-is-the-simplest-way-to-make-tooltips-
in-tkinter/36221216#36221216
http://www.daniweb.com/programming/software-development/
code/484591/a-tooltip-class-for-tkinter
- Originally written by vegaseat on 2014.09.09.
- Modified to include a delay time by Victor Zaccardo on 2016.03.25.
- Modified
- to correct extreme right and extreme bottom behavior,
- to stay inside the screen whenever the tooltip might go out on
the top but still the screen is higher than the tooltip,
- to use the more flexible mouse positioning,
- to add customizable background color, padding, waittime and
wraplength on creation
by Alberto Vassena on 2016.11.05.
Tested on Ubuntu 16.04/16.10, running Python 3.5.2
TODO: themes styles support
'''
def __init__(self,
widget,
bg='#FFFFEA',
pad=(5, 3, 5, 3),
text='widget info',
waittime=400,
wraplength=300):
self.waittime = waittime # in miliseconds, originally 500
self.wraplength = wraplength # in pixels, originally 180
self.widget = widget
self.text = text
self.widget.bind("<Enter>", self.onEnter)
self.widget.bind("<FocusIn>", self.onEnter)
self.widget.bind("<FocusOut>", self.onLeave)
self.widget.bind("<Leave>", self.onLeave)
self.widget.bind("<ButtonPress>", self.onLeave)
self.bg = bg
self.pad = pad
self.id = None
self.tw = None
def onEnter(self, event=None):
self.schedule()
def onLeave(self, event=None):
self.unschedule()
self.hide()
def schedule(self):
self.unschedule()
self.id = self.widget.after(self.waittime, self.show)
def unschedule(self):
id_ = self.id
self.id = None
if id_:
self.widget.after_cancel(id_)
def show(self):
def tip_pos_calculator(widget,
label,
tip_delta=(10, 5),
pad=(5, 3, 5, 3)):
w = widget
s_width, s_height = w.winfo_screenwidth(), w.winfo_screenheight()
width, height = (pad[0] + label.winfo_reqwidth() + pad[2],
pad[1] + label.winfo_reqheight() + pad[3])
mouse_x, mouse_y = w.winfo_pointerxy()
x1, y1 = mouse_x + tip_delta[0], mouse_y + tip_delta[1]
x2, y2 = x1 + width, y1 + height
x_delta = x2 - s_width
if x_delta < 0:
x_delta = 0
y_delta = y2 - s_height
if y_delta < 0:
y_delta = 0
offscreen = (x_delta, y_delta) != (0, 0)
if offscreen:
if x_delta:
x1 = mouse_x - tip_delta[0] - width
if y_delta:
y1 = mouse_y - tip_delta[1] - height
offscreen_again = y1 < 0 # out on the top
if offscreen_again:
# No further checks will be done.
# TIP:
# A further mod might automagically augment the
# wraplength when the tooltip is too high to be
# kept inside the screen.
y1 = 0
return x1, y1
bg = self.bg
pad = self.pad
widget = self.widget
# creates a toplevel window
self.tw = Toplevel(widget)
# Leaves only the label and removes the app window
self.tw.wm_overrideredirect(True)
win = Frame(self.tw, background=bg, borderwidth=0)
label = Label(
win,
text=self.text,
justify=LEFT,
background=bg,
relief=SOLID,
borderwidth=0,
wraplength=self.wraplength)
label.grid(padx=(pad[0], pad[2]), pady=(pad[1], pad[3]), sticky=NSEW)
win.grid()
x, y = tip_pos_calculator(widget, label)
self.tw.wm_geometry("+%d+%d" % (x, y))
def hide(self):
tw = self.tw
if tw:
tw.destroy()
self.tw = None