ยปCore Development>Code coverage>Lib/idlelib/tooltip.py

Python code coverage for Lib/idlelib/tooltip.py

#countcontent
1n/a# general purpose 'tooltip' routines - currently unused in idlelib
2n/a# (although the 'calltips' extension is partly based on this code)
3n/a# may be useful for some purposes in (or almost in ;) the current project scope
4n/a# Ideas gleaned from PySol
5n/a
6n/afrom tkinter import *
7n/a
8n/aclass ToolTipBase:
9n/a
10n/a def __init__(self, button):
11n/a self.button = button
12n/a self.tipwindow = None
13n/a self.id = None
14n/a self.x = self.y = 0
15n/a self._id1 = self.button.bind("<Enter>", self.enter)
16n/a self._id2 = self.button.bind("<Leave>", self.leave)
17n/a self._id3 = self.button.bind("<ButtonPress>", self.leave)
18n/a
19n/a def enter(self, event=None):
20n/a self.schedule()
21n/a
22n/a def leave(self, event=None):
23n/a self.unschedule()
24n/a self.hidetip()
25n/a
26n/a def schedule(self):
27n/a self.unschedule()
28n/a self.id = self.button.after(1500, self.showtip)
29n/a
30n/a def unschedule(self):
31n/a id = self.id
32n/a self.id = None
33n/a if id:
34n/a self.button.after_cancel(id)
35n/a
36n/a def showtip(self):
37n/a if self.tipwindow:
38n/a return
39n/a # The tip window must be completely outside the button;
40n/a # otherwise when the mouse enters the tip window we get
41n/a # a leave event and it disappears, and then we get an enter
42n/a # event and it reappears, and so on forever :-(
43n/a x = self.button.winfo_rootx() + 20
44n/a y = self.button.winfo_rooty() + self.button.winfo_height() + 1
45n/a self.tipwindow = tw = Toplevel(self.button)
46n/a tw.wm_overrideredirect(1)
47n/a tw.wm_geometry("+%d+%d" % (x, y))
48n/a self.showcontents()
49n/a
50n/a def showcontents(self, text="Your text here"):
51n/a # Override this in derived class
52n/a label = Label(self.tipwindow, text=text, justify=LEFT,
53n/a background="#ffffe0", relief=SOLID, borderwidth=1)
54n/a label.pack()
55n/a
56n/a def hidetip(self):
57n/a tw = self.tipwindow
58n/a self.tipwindow = None
59n/a if tw:
60n/a tw.destroy()
61n/a
62n/aclass ToolTip(ToolTipBase):
63n/a def __init__(self, button, text):
64n/a ToolTipBase.__init__(self, button)
65n/a self.text = text
66n/a def showcontents(self):
67n/a ToolTipBase.showcontents(self, self.text)
68n/a
69n/aclass ListboxToolTip(ToolTipBase):
70n/a def __init__(self, button, items):
71n/a ToolTipBase.__init__(self, button)
72n/a self.items = items
73n/a def showcontents(self):
74n/a listbox = Listbox(self.tipwindow, background="#ffffe0")
75n/a listbox.pack()
76n/a for item in self.items:
77n/a listbox.insert(END, item)
78n/a
79n/adef _tooltip(parent): # htest #
80n/a top = Toplevel(parent)
81n/a top.title("Test tooltip")
82n/a x, y = map(int, parent.geometry().split('+')[1:])
83n/a top.geometry("+%d+%d" % (x, y + 150))
84n/a label = Label(top, text="Place your mouse over buttons")
85n/a label.pack()
86n/a button1 = Button(top, text="Button 1")
87n/a button2 = Button(top, text="Button 2")
88n/a button1.pack()
89n/a button2.pack()
90n/a ToolTip(button1, "This is tooltip text for button1.")
91n/a ListboxToolTip(button2, ["This is","multiple line",
92n/a "tooltip text","for button2"])
93n/a
94n/aif __name__ == '__main__':
95n/a from idlelib.idle_test.htest import run
96n/a run(_tooltip)