blob: 843fb4a7d0b7419884f07a07ad92be6a33b6294e [file] [log] [blame]
Terry Jan Reedy6fa5bdc2016-05-28 13:22:31 -04001# general purpose 'tooltip' routines - currently unused in idlelib
Kurt B. Kaiserb3705a32002-09-14 02:56:04 +00002# (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
David Scherer7aced172000-08-15 01:13:23 +00004# Ideas gleaned from PySol
5
Georg Brandl14fc4272008-05-17 18:39:55 +00006from tkinter import *
David Scherer7aced172000-08-15 01:13:23 +00007
8class ToolTipBase:
9
10 def __init__(self, button):
11 self.button = button
12 self.tipwindow = None
13 self.id = None
14 self.x = self.y = 0
15 self._id1 = self.button.bind("<Enter>", self.enter)
16 self._id2 = self.button.bind("<Leave>", self.leave)
17 self._id3 = self.button.bind("<ButtonPress>", self.leave)
18
19 def enter(self, event=None):
20 self.schedule()
21
22 def leave(self, event=None):
23 self.unschedule()
24 self.hidetip()
25
26 def schedule(self):
27 self.unschedule()
28 self.id = self.button.after(1500, self.showtip)
29
30 def unschedule(self):
31 id = self.id
32 self.id = None
33 if id:
34 self.button.after_cancel(id)
35
36 def showtip(self):
37 if self.tipwindow:
38 return
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))
48 self.showcontents()
49
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)
54 label.pack()
55
56 def hidetip(self):
57 tw = self.tipwindow
58 self.tipwindow = None
59 if tw:
60 tw.destroy()
61
62class ToolTip(ToolTipBase):
63 def __init__(self, button, text):
64 ToolTipBase.__init__(self, button)
65 self.text = text
66 def showcontents(self):
67 ToolTipBase.showcontents(self, self.text)
68
69class ListboxToolTip(ToolTipBase):
70 def __init__(self, button, items):
71 ToolTipBase.__init__(self, button)
72 self.items = items
73 def showcontents(self):
74 listbox = Listbox(self.tipwindow, background="#ffffe0")
75 listbox.pack()
76 for item in self.items:
77 listbox.insert(END, item)
78
Terry Jan Reedy6fa5bdc2016-05-28 13:22:31 -040079def _tooltip(parent): # htest #
Terry Jan Reedya7480322016-07-10 17:28:10 -040080 top = Toplevel(parent)
81 top.title("Test tooltip")
82 x, y = map(int, parent.geometry().split('+')[1:])
83 top.geometry("+%d+%d" % (x, y + 150))
84 label = Label(top, text="Place your mouse over buttons")
Terry Jan Reedy1b392ff2014-05-24 18:48:18 -040085 label.pack()
Terry Jan Reedya7480322016-07-10 17:28:10 -040086 button1 = Button(top, text="Button 1")
87 button2 = Button(top, text="Button 2")
Terry Jan Reedy1b392ff2014-05-24 18:48:18 -040088 button1.pack()
89 button2.pack()
Terry Jan Reedya2fc99e2014-05-25 18:44:05 -040090 ToolTip(button1, "This is tooltip text for button1.")
91 ListboxToolTip(button2, ["This is","multiple line",
92 "tooltip text","for button2"])
Kurt B. Kaiserd5338a82001-07-14 01:14:09 +000093
Kurt B. Kaiser9df23ea2005-11-23 15:12:19 +000094if __name__ == '__main__':
Terry Jan Reedy1b392ff2014-05-24 18:48:18 -040095 from idlelib.idle_test.htest import run
96 run(_tooltip)