blob: db1e346c55dc54433299c042695d7bb31629af1e [file] [log] [blame]
Guido van Rossum85b97351999-06-02 16:10:19 +00001# A CallTip window class for Tkinter/IDLE.
2# After ToolTip.py, which uses ideas gleaned from PySol
3
4# Used by the CallTips IDLE extension.
5import os
6from Tkinter import *
7
8class CallTip:
9
10 def __init__(self, widget):
11 self.widget = widget
12 self.tipwindow = None
13 self.id = None
14 self.x = self.y = 0
15
16 def showtip(self, text):
Tim Peters32b069c2002-04-22 18:43:49 +000017 # SF bug 546078: IDLE calltips cause application error.
18 # There were crashes on various Windows flavors, and even a
19 # crashing X server on Linux, with very long calltips.
20 if len(text) >= 79:
21 text = text[:75] + ' ...'
Guido van Rossum85b97351999-06-02 16:10:19 +000022 self.text = text
Tim Peters32b069c2002-04-22 18:43:49 +000023
Guido van Rossum85b97351999-06-02 16:10:19 +000024 if self.tipwindow or not self.text:
25 return
26 self.widget.see("insert")
27 x, y, cx, cy = self.widget.bbox("insert")
28 x = x + self.widget.winfo_rootx() + 2
29 y = y + cy + self.widget.winfo_rooty()
30 self.tipwindow = tw = Toplevel(self.widget)
31 tw.wm_overrideredirect(1)
32 tw.wm_geometry("+%d+%d" % (x, y))
33 label = Label(tw, text=self.text, justify=LEFT,
Guido van Rossumab3b50b1999-06-10 14:19:46 +000034 background="#ffffe0", relief=SOLID, borderwidth=1,
35 font = self.widget['font'])
Guido van Rossum85b97351999-06-02 16:10:19 +000036 label.pack()
Tim Peters70c43782001-01-17 08:48:39 +000037
Guido van Rossum85b97351999-06-02 16:10:19 +000038 def hidetip(self):
39 tw = self.tipwindow
40 self.tipwindow = None
41 if tw:
42 tw.destroy()
43
44
45###############################
46#
47# Test Code
48#
49class container: # Conceptually an editor_window
50 def __init__(self):
51 root = Tk()
52 text = self.text = Text(root)
53 text.pack(side=LEFT, fill=BOTH, expand=1)
54 text.insert("insert", "string.split")
55 root.update()
56 self.calltip = CallTip(text)
57
58 text.event_add("<<calltip-show>>", "(")
59 text.event_add("<<calltip-hide>>", ")")
60 text.bind("<<calltip-show>>", self.calltip_show)
61 text.bind("<<calltip-hide>>", self.calltip_hide)
Tim Peters70c43782001-01-17 08:48:39 +000062
Guido van Rossum85b97351999-06-02 16:10:19 +000063 text.focus_set()
64 # root.mainloop() # not in idle
65
66 def calltip_show(self, event):
67 self.calltip.showtip("Hello world")
68
69 def calltip_hide(self, event):
70 self.calltip.hidetip()
71
72def main():
73 # Test code
74 c=container()
75
76if __name__=='__main__':
77 main()