ToolTip.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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
  5. from Tkinter import *
  6. class ToolTipBase:
  7. def __init__(self, button):
  8. self.button = button
  9. self.tipwindow = None
  10. self.id = None
  11. self.x = self.y = 0
  12. self._id1 = self.button.bind("<Enter>", self.enter)
  13. self._id2 = self.button.bind("<Leave>", self.leave)
  14. self._id3 = self.button.bind("<ButtonPress>", self.leave)
  15. def enter(self, event=None):
  16. self.schedule()
  17. def leave(self, event=None):
  18. self.unschedule()
  19. self.hidetip()
  20. def schedule(self):
  21. self.unschedule()
  22. self.id = self.button.after(1500, self.showtip)
  23. def unschedule(self):
  24. id = self.id
  25. self.id = None
  26. if id:
  27. self.button.after_cancel(id)
  28. def showtip(self):
  29. if self.tipwindow:
  30. return
  31. # The tip window must be completely outside the button;
  32. # otherwise when the mouse enters the tip window we get
  33. # a leave event and it disappears, and then we get an enter
  34. # event and it reappears, and so on forever :-(
  35. x = self.button.winfo_rootx() + 20
  36. y = self.button.winfo_rooty() + self.button.winfo_height() + 1
  37. self.tipwindow = tw = Toplevel(self.button)
  38. tw.wm_overrideredirect(1)
  39. tw.wm_geometry("+%d+%d" % (x, y))
  40. self.showcontents()
  41. def showcontents(self, text="Your text here"):
  42. # Override this in derived class
  43. label = Label(self.tipwindow, text=text, justify=LEFT,
  44. background="#ffffe0", relief=SOLID, borderwidth=1)
  45. label.pack()
  46. def hidetip(self):
  47. tw = self.tipwindow
  48. self.tipwindow = None
  49. if tw:
  50. tw.destroy()
  51. class ToolTip(ToolTipBase):
  52. def __init__(self, button, text):
  53. ToolTipBase.__init__(self, button)
  54. self.text = text
  55. def showcontents(self):
  56. ToolTipBase.showcontents(self, self.text)
  57. class ListboxToolTip(ToolTipBase):
  58. def __init__(self, button, items):
  59. ToolTipBase.__init__(self, button)
  60. self.items = items
  61. def showcontents(self):
  62. listbox = Listbox(self.tipwindow, background="#ffffe0")
  63. listbox.pack()
  64. for item in self.items:
  65. listbox.insert(END, item)
  66. def _tooltip(parent):
  67. root = Tk()
  68. root.title("Test tooltip")
  69. width, height, x, y = list(map(int, re.split('[x+]', parent.geometry())))
  70. root.geometry("+%d+%d"%(x, y + 150))
  71. label = Label(root, text="Place your mouse over buttons")
  72. label.pack()
  73. button1 = Button(root, text="Button 1")
  74. button2 = Button(root, text="Button 2")
  75. button1.pack()
  76. button2.pack()
  77. ToolTip(button1, "This is tooltip text for button1.")
  78. ListboxToolTip(button2, ["This is","multiple line",
  79. "tooltip text","for button2"])
  80. root.mainloop()
  81. if __name__ == '__main__':
  82. from idlelib.idle_test.htest import run
  83. run(_tooltip)