-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtiphelper.py
175 lines (108 loc) · 4.98 KB
/
tiphelper.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
#!/usr/bin/env python3
"""
This file is part of Notelocked.
Copyright (C) 2022 Leonardo A. Reichert
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
"""
#you can see the full license at: "LICENSE" file
#https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
# Contact: [email protected]
#try:
from tkinter import Toplevel,Label;
#except ImportError:
# from Tkinter import Toplevel,Label;
class TipHelper(Toplevel):
""" A small label text helper, appears by cursor mouse enter in a widget """
def __init__(self, master):
Toplevel.__init__(self, master, relief="solid", bd=1);
self.withdraw(); #invisible
self.overrideredirect(True); #no-border or title
self.label = Label(self, bg="#FFFFA0"); #yellowed butter color :)
self.label.pack(side="top", fill="both", expand="yes");
self._lastEventId = 0;
def putOn(self, widget, text, anchor="s", _wait=1500):
""" put a event enter/leave by user mouse in a widget"""
#add = True -> (no replace a posible previous event)
widget.bind("<Enter>",lambda e: self._enterOn(e, text, anchor, _wait), add=True);
widget.bind("<Leave>",lambda e: self._showOff(), add=True);
widget.bind("<ButtonPress>",lambda e: self._showOff(), add=True);
assert anchor in ("s", "n"), "anchor invalid"
def _enterOn(self, e, text, anchor, _wait):
"""intern, parse event enter by user mouse"""
def showOn(): #temporalized
self.label.configure(text=text);
wmid = self.winfo_width()//2;
x = self.winfo_pointerx()-wmid; #mouse x - width//2
height = max(self.winfo_height(), 24);
hmid = max(height, 24)//2;
if anchor == "n":
y = e.widget.winfo_rooty()-height-5;
elif anchor == "s":
y = e.widget.winfo_rooty()+e.widget.winfo_height()+5; #y2 (Bottom)
#elif...
else:
y = self.winfo_pointery()+hmid; #mouse y + height//2
self.geometry("+%d+%d" % (x,y));
self.deiconify(); #show
self._lastEventId = self.after(_wait, showOn); #need mouse 1500ms inside of widget
def showNow(self, widget, text, anchor, duration_ms=1500):
"""
Show label on a widget now inmediatelly, without events.
Cannot use after .putOn(..) method was used before,
need create other instance """
assert anchor in ("w", "s"), "ivalid anchor";
if self._lastEventId:
self.after_cancel(self._lastEventId);
self.label.configure(text=text);
widget.bind("<ButtonPress>",lambda e: self.withdraw(), add=True);
x = widget.winfo_rootx();
y = widget.winfo_rooty();
if anchor == "w":
width = widget.winfo_width();
self.geometry("+%d+%d" % (x+width+2,y));
elif anchor == "s":
height = widget.winfo_height();
self.geometry("+%d+%d" % (x,y+height+2));
#elif...
self.deiconify(); #show
self._lastEventId = self.after(duration_ms, self.withdraw); #event wait for hidden
def _showOff(self):
"""intern, parse event leave by user mouse"""
self.after_cancel(self._lastEventId);
self.withdraw(); #invisible
def __str__(self):
return "tiphelper"
if __name__ == "__main__":
#try:
from tkinter import Tk, Button;
#except:
# from Tkinter import Tk, Button;
root = Tk();
root.title("testing TipHelper");
root.geometry("350x300");
def clic():
tip2.showNow(button, "<- by clic", "w");
button = Button(root, text="1- click me", relief="solid", command=clic);
button.pack(side="top", pady=10);
button2 = Button(root, text="2- enter mouse here", relief="solid");
button2.pack(side="top", pady=10);
button3 = Button(root, text="3 - and here", relief="solid");
button3.pack(side="top", pady=10);
button4 = Button(root, text="4 - now fast", relief="solid");
button4.pack(side="top", pady=10);
tip = TipHelper(root);
tip2 = TipHelper(root);
tip.putOn(button2, "this label is the effect test", "s");
tip.putOn(button3, "this label\ncontain multiple lines", "s");
tip.putOn(button4, "this label\ncontain multiple lines", "s", 200);
root.mainloop();