1 | n/a | """A CallTip window class for Tkinter/IDLE. |
---|
2 | n/a | |
---|
3 | n/a | After tooltip.py, which uses ideas gleaned from PySol |
---|
4 | n/a | Used by the calltips IDLE extension. |
---|
5 | n/a | """ |
---|
6 | n/a | from tkinter import Toplevel, Label, LEFT, SOLID, TclError |
---|
7 | n/a | |
---|
8 | n/a | HIDE_VIRTUAL_EVENT_NAME = "<<calltipwindow-hide>>" |
---|
9 | n/a | HIDE_SEQUENCES = ("<Key-Escape>", "<FocusOut>") |
---|
10 | n/a | CHECKHIDE_VIRTUAL_EVENT_NAME = "<<calltipwindow-checkhide>>" |
---|
11 | n/a | CHECKHIDE_SEQUENCES = ("<KeyRelease>", "<ButtonRelease>") |
---|
12 | n/a | CHECKHIDE_TIME = 100 # milliseconds |
---|
13 | n/a | |
---|
14 | n/a | MARK_RIGHT = "calltipwindowregion_right" |
---|
15 | n/a | |
---|
16 | n/a | class CallTip: |
---|
17 | n/a | |
---|
18 | n/a | def __init__(self, widget): |
---|
19 | n/a | self.widget = widget |
---|
20 | n/a | self.tipwindow = self.label = None |
---|
21 | n/a | self.parenline = self.parencol = None |
---|
22 | n/a | self.lastline = None |
---|
23 | n/a | self.hideid = self.checkhideid = None |
---|
24 | n/a | self.checkhide_after_id = None |
---|
25 | n/a | |
---|
26 | n/a | def position_window(self): |
---|
27 | n/a | """Check if needs to reposition the window, and if so - do it.""" |
---|
28 | n/a | curline = int(self.widget.index("insert").split('.')[0]) |
---|
29 | n/a | if curline == self.lastline: |
---|
30 | n/a | return |
---|
31 | n/a | self.lastline = curline |
---|
32 | n/a | self.widget.see("insert") |
---|
33 | n/a | if curline == self.parenline: |
---|
34 | n/a | box = self.widget.bbox("%d.%d" % (self.parenline, |
---|
35 | n/a | self.parencol)) |
---|
36 | n/a | else: |
---|
37 | n/a | box = self.widget.bbox("%d.0" % curline) |
---|
38 | n/a | if not box: |
---|
39 | n/a | box = list(self.widget.bbox("insert")) |
---|
40 | n/a | # align to left of window |
---|
41 | n/a | box[0] = 0 |
---|
42 | n/a | box[2] = 0 |
---|
43 | n/a | x = box[0] + self.widget.winfo_rootx() + 2 |
---|
44 | n/a | y = box[1] + box[3] + self.widget.winfo_rooty() |
---|
45 | n/a | self.tipwindow.wm_geometry("+%d+%d" % (x, y)) |
---|
46 | n/a | |
---|
47 | n/a | def showtip(self, text, parenleft, parenright): |
---|
48 | n/a | """Show the calltip, bind events which will close it and reposition it. |
---|
49 | n/a | """ |
---|
50 | n/a | # Only called in CallTips, where lines are truncated |
---|
51 | n/a | self.text = text |
---|
52 | n/a | if self.tipwindow or not self.text: |
---|
53 | n/a | return |
---|
54 | n/a | |
---|
55 | n/a | self.widget.mark_set(MARK_RIGHT, parenright) |
---|
56 | n/a | self.parenline, self.parencol = map( |
---|
57 | n/a | int, self.widget.index(parenleft).split(".")) |
---|
58 | n/a | |
---|
59 | n/a | self.tipwindow = tw = Toplevel(self.widget) |
---|
60 | n/a | self.position_window() |
---|
61 | n/a | # remove border on calltip window |
---|
62 | n/a | tw.wm_overrideredirect(1) |
---|
63 | n/a | try: |
---|
64 | n/a | # This command is only needed and available on Tk >= 8.4.0 for OSX |
---|
65 | n/a | # Without it, call tips intrude on the typing process by grabbing |
---|
66 | n/a | # the focus. |
---|
67 | n/a | tw.tk.call("::tk::unsupported::MacWindowStyle", "style", tw._w, |
---|
68 | n/a | "help", "noActivates") |
---|
69 | n/a | except TclError: |
---|
70 | n/a | pass |
---|
71 | n/a | self.label = Label(tw, text=self.text, justify=LEFT, |
---|
72 | n/a | background="#ffffe0", relief=SOLID, borderwidth=1, |
---|
73 | n/a | font = self.widget['font']) |
---|
74 | n/a | self.label.pack() |
---|
75 | n/a | tw.lift() # work around bug in Tk 8.5.18+ (issue #24570) |
---|
76 | n/a | |
---|
77 | n/a | self.checkhideid = self.widget.bind(CHECKHIDE_VIRTUAL_EVENT_NAME, |
---|
78 | n/a | self.checkhide_event) |
---|
79 | n/a | for seq in CHECKHIDE_SEQUENCES: |
---|
80 | n/a | self.widget.event_add(CHECKHIDE_VIRTUAL_EVENT_NAME, seq) |
---|
81 | n/a | self.widget.after(CHECKHIDE_TIME, self.checkhide_event) |
---|
82 | n/a | self.hideid = self.widget.bind(HIDE_VIRTUAL_EVENT_NAME, |
---|
83 | n/a | self.hide_event) |
---|
84 | n/a | for seq in HIDE_SEQUENCES: |
---|
85 | n/a | self.widget.event_add(HIDE_VIRTUAL_EVENT_NAME, seq) |
---|
86 | n/a | |
---|
87 | n/a | def checkhide_event(self, event=None): |
---|
88 | n/a | if not self.tipwindow: |
---|
89 | n/a | # If the event was triggered by the same event that unbinded |
---|
90 | n/a | # this function, the function will be called nevertheless, |
---|
91 | n/a | # so do nothing in this case. |
---|
92 | n/a | return |
---|
93 | n/a | curline, curcol = map(int, self.widget.index("insert").split('.')) |
---|
94 | n/a | if curline < self.parenline or \ |
---|
95 | n/a | (curline == self.parenline and curcol <= self.parencol) or \ |
---|
96 | n/a | self.widget.compare("insert", ">", MARK_RIGHT): |
---|
97 | n/a | self.hidetip() |
---|
98 | n/a | else: |
---|
99 | n/a | self.position_window() |
---|
100 | n/a | if self.checkhide_after_id is not None: |
---|
101 | n/a | self.widget.after_cancel(self.checkhide_after_id) |
---|
102 | n/a | self.checkhide_after_id = \ |
---|
103 | n/a | self.widget.after(CHECKHIDE_TIME, self.checkhide_event) |
---|
104 | n/a | |
---|
105 | n/a | def hide_event(self, event): |
---|
106 | n/a | if not self.tipwindow: |
---|
107 | n/a | # See the explanation in checkhide_event. |
---|
108 | n/a | return |
---|
109 | n/a | self.hidetip() |
---|
110 | n/a | |
---|
111 | n/a | def hidetip(self): |
---|
112 | n/a | if not self.tipwindow: |
---|
113 | n/a | return |
---|
114 | n/a | |
---|
115 | n/a | for seq in CHECKHIDE_SEQUENCES: |
---|
116 | n/a | self.widget.event_delete(CHECKHIDE_VIRTUAL_EVENT_NAME, seq) |
---|
117 | n/a | self.widget.unbind(CHECKHIDE_VIRTUAL_EVENT_NAME, self.checkhideid) |
---|
118 | n/a | self.checkhideid = None |
---|
119 | n/a | for seq in HIDE_SEQUENCES: |
---|
120 | n/a | self.widget.event_delete(HIDE_VIRTUAL_EVENT_NAME, seq) |
---|
121 | n/a | self.widget.unbind(HIDE_VIRTUAL_EVENT_NAME, self.hideid) |
---|
122 | n/a | self.hideid = None |
---|
123 | n/a | |
---|
124 | n/a | self.label.destroy() |
---|
125 | n/a | self.label = None |
---|
126 | n/a | self.tipwindow.destroy() |
---|
127 | n/a | self.tipwindow = None |
---|
128 | n/a | |
---|
129 | n/a | self.widget.mark_unset(MARK_RIGHT) |
---|
130 | n/a | self.parenline = self.parencol = self.lastline = None |
---|
131 | n/a | |
---|
132 | n/a | def is_active(self): |
---|
133 | n/a | return bool(self.tipwindow) |
---|
134 | n/a | |
---|
135 | n/a | |
---|
136 | n/a | def _calltip_window(parent): # htest # |
---|
137 | n/a | from tkinter import Toplevel, Text, LEFT, BOTH |
---|
138 | n/a | |
---|
139 | n/a | top = Toplevel(parent) |
---|
140 | n/a | top.title("Test calltips") |
---|
141 | n/a | x, y = map(int, parent.geometry().split('+')[1:]) |
---|
142 | n/a | top.geometry("200x100+%d+%d" % (x + 250, y + 175)) |
---|
143 | n/a | text = Text(top) |
---|
144 | n/a | text.pack(side=LEFT, fill=BOTH, expand=1) |
---|
145 | n/a | text.insert("insert", "string.split") |
---|
146 | n/a | top.update() |
---|
147 | n/a | calltip = CallTip(text) |
---|
148 | n/a | |
---|
149 | n/a | def calltip_show(event): |
---|
150 | n/a | calltip.showtip("(s=Hello world)", "insert", "end") |
---|
151 | n/a | def calltip_hide(event): |
---|
152 | n/a | calltip.hidetip() |
---|
153 | n/a | text.event_add("<<calltip-show>>", "(") |
---|
154 | n/a | text.event_add("<<calltip-hide>>", ")") |
---|
155 | n/a | text.bind("<<calltip-show>>", calltip_show) |
---|
156 | n/a | text.bind("<<calltip-hide>>", calltip_hide) |
---|
157 | n/a | text.focus_set() |
---|
158 | n/a | |
---|
159 | n/a | if __name__=='__main__': |
---|
160 | n/a | from idlelib.idle_test.htest import run |
---|
161 | n/a | run(_calltip_window) |
---|