Fix three PyChecker-detected gotchas.
[python/dscho.git] / Lib / idlelib / ToolTip.py
blob691f5101c1633969d28c4e520a39f028b30a4e1a
1 # Ideas gleaned from PySol
3 import os
4 from Tkinter import *
6 class ToolTipBase:
8 def __init__(self, button):
9 self.button = button
10 self.tipwindow = None
11 self.id = None
12 self.x = self.y = 0
13 self._id1 = self.button.bind("<Enter>", self.enter)
14 self._id2 = self.button.bind("<Leave>", self.leave)
15 self._id3 = self.button.bind("<ButtonPress>", self.leave)
17 def enter(self, event=None):
18 self.schedule()
20 def leave(self, event=None):
21 self.unschedule()
22 self.hidetip()
24 def schedule(self):
25 self.unschedule()
26 self.id = self.button.after(1500, self.showtip)
28 def unschedule(self):
29 id = self.id
30 self.id = None
31 if id:
32 self.button.after_cancel(id)
34 def showtip(self):
35 if self.tipwindow:
36 return
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))
46 self.showcontents()
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)
52 label.pack()
54 def hidetip(self):
55 tw = self.tipwindow
56 self.tipwindow = None
57 if tw:
58 tw.destroy()
60 class ToolTip(ToolTipBase):
61 def __init__(self, button, text):
62 ToolTipBase.__init__(self, button)
63 self.text = text
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)
70 self.items = items
71 def showcontents(self):
72 listbox = Listbox(self.tipwindow, background="#ffffe0")
73 listbox.pack()
74 for item in self.items:
75 listbox.insert(END, item)
77 def main():
78 # Test code
79 root = Tk()
80 b = Button(root, text="Hello", command=root.destroy)
81 b.pack()
82 root.update()
83 tip = ListboxToolTip(b, ["Hello", "world"])
85 # root.mainloop() # not in idle
87 main()