1 # general purpose 'tooltip' routines - currently unused in idlefork
2 # (although the 'calltips' extension is partly based on this code)
3 # may be useful for some purposes in (or almost in ;) the current project scope
4 # Ideas gleaned from PySol
10 def __init__(self
, button
):
15 self
._id
1 = self
.button
.bind("<Enter>", self
.enter
)
16 self
._id
2 = self
.button
.bind("<Leave>", self
.leave
)
17 self
._id
3 = self
.button
.bind("<ButtonPress>", self
.leave
)
19 def enter(self
, event
=None):
22 def leave(self
, event
=None):
28 self
.id = self
.button
.after(1500, self
.showtip
)
34 self
.button
.after_cancel(id)
39 # The tip window must be completely outside the button;
40 # otherwise when the mouse enters the tip window we get
41 # a leave event and it disappears, and then we get an enter
42 # event and it reappears, and so on forever :-(
43 x
= self
.button
.winfo_rootx() + 20
44 y
= self
.button
.winfo_rooty() + self
.button
.winfo_height() + 1
45 self
.tipwindow
= tw
= Toplevel(self
.button
)
46 tw
.wm_overrideredirect(1)
47 tw
.wm_geometry("+%d+%d" % (x
, y
))
50 def showcontents(self
, text
="Your text here"):
51 # Override this in derived class
52 label
= Label(self
.tipwindow
, text
=text
, justify
=LEFT
,
53 background
="#ffffe0", relief
=SOLID
, borderwidth
=1)
62 class ToolTip(ToolTipBase
):
63 def __init__(self
, button
, text
):
64 ToolTipBase
.__init
__(self
, button
)
66 def showcontents(self
):
67 ToolTipBase
.showcontents(self
, self
.text
)
69 class ListboxToolTip(ToolTipBase
):
70 def __init__(self
, button
, items
):
71 ToolTipBase
.__init
__(self
, button
)
73 def showcontents(self
):
74 listbox
= Listbox(self
.tipwindow
, background
="#ffffe0")
76 for item
in self
.items
:
77 listbox
.insert(END
, item
)
82 b
= Button(root
, text
="Hello", command
=root
.destroy
)
85 tip
= ListboxToolTip(b
, ["Hello", "world"])
88 if __name__
== '__main__':