Guido van Rossum | d3a518b | 1999-06-01 18:19:02 +0000 | [diff] [blame] | 1 | # Ideas gleaned from PySol |
| 2 | |
| 3 | import os |
| 4 | from Tkinter import * |
| 5 | |
| 6 | class ToolTipBase: |
| 7 | |
| 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) |
| 16 | |
| 17 | def enter(self, event=None): |
| 18 | self.schedule() |
| 19 | |
| 20 | def leave(self, event=None): |
| 21 | self.unschedule() |
| 22 | self.hidetip() |
| 23 | |
| 24 | def schedule(self): |
| 25 | self.unschedule() |
| 26 | self.id = self.button.after(1500, self.showtip) |
| 27 | |
| 28 | def unschedule(self): |
| 29 | id = self.id |
| 30 | self.id = None |
| 31 | if id: |
| 32 | self.button.after_cancel(id) |
| 33 | |
| 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() |
| 47 | |
| 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() |
| 53 | |
| 54 | def hidetip(self): |
| 55 | tw = self.tipwindow |
| 56 | self.tipwindow = None |
| 57 | if tw: |
| 58 | tw.destroy() |
| 59 | |
| 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) |
| 66 | |
| 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) |
| 76 | |
| 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"]) |
| 84 | |
| 85 | # root.mainloop() # not in idle |
| 86 | |
| 87 | main() |