1 # Ideas gleaned from PySol
8 def __init__(self
, button
):
13 self
._id
1 = self
.button
.bind("<Enter>", self
.enter
)
14 self
._id
2 = self
.button
.bind("<Leave>", self
.leave
)
15 self
._id
3 = self
.button
.bind("<ButtonPress>", self
.leave
)
17 def enter(self
, event
=None):
20 def leave(self
, event
=None):
26 self
.id = self
.button
.after(1500, self
.showtip
)
32 self
.button
.after_cancel(id)
37 # The tip window must be completely outside the button;
38 # otherwise when the mouse enters the tip window we get
39 # a leave event and it disappears, and then we get an enter
40 # event and it reappears, and so on forever :-(
41 x
= self
.button
.winfo_rootx() + 20
42 y
= self
.button
.winfo_rooty() + self
.button
.winfo_height() + 1
43 self
.tipwindow
= tw
= Toplevel(self
.button
)
44 tw
.wm_overrideredirect(1)
45 tw
.wm_geometry("+%d+%d" % (x
, y
))
48 def showcontents(self
, text
="Your text here"):
49 # Override this in derived class
50 label
= Label(self
.tipwindow
, text
=text
, justify
=LEFT
,
51 background
="#ffffe0", relief
=SOLID
, borderwidth
=1)
60 class ToolTip(ToolTipBase
):
61 def __init__(self
, button
, text
):
62 ToolTipBase
.__init
__(self
, button
)
64 def showcontents(self
):
65 ToolTipBase
.showcontents(self
, self
.text
)
67 class ListboxToolTip(ToolTipBase
):
68 def __init__(self
, button
, items
):
69 ToolTipBase
.__init
__(self
, button
)
71 def showcontents(self
):
72 listbox
= Listbox(self
.tipwindow
, background
="#ffffe0")
74 for item
in self
.items
:
75 listbox
.insert(END
, item
)
80 b
= Button(root
, text
="Hello", command
=root
.destroy
)
83 tip
= ListboxToolTip(b
, ["Hello", "world"])
85 # root.mainloop() # not in idle