blob: 1542fe7cff4ccb82f709ec466b01442fd6d88086 [file] [log] [blame]
David Scherer7aced172000-08-15 01:13:23 +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.
David Scherer7aced172000-08-15 01:13:23 +00005from Tkinter import *
6
7class CallTip:
8
9 def __init__(self, widget):
10 self.widget = widget
11 self.tipwindow = None
12 self.id = None
13 self.x = self.y = 0
14
15 def showtip(self, text):
Kurt B. Kaisere72f05d2002-09-15 21:43:13 +000016 # SF bug 546078: IDLE calltips cause application error.
17 # There were crashes on various Windows flavors, and even a
18 # crashing X server on Linux, with very long calltips.
19 if len(text) >= 79:
20 text = text[:75] + ' ...'
David Scherer7aced172000-08-15 01:13:23 +000021 self.text = text
Kurt B. Kaisere72f05d2002-09-15 21:43:13 +000022
David Scherer7aced172000-08-15 01:13:23 +000023 if self.tipwindow or not self.text:
24 return
25 self.widget.see("insert")
26 x, y, cx, cy = self.widget.bbox("insert")
27 x = x + self.widget.winfo_rootx() + 2
28 y = y + cy + self.widget.winfo_rooty()
29 self.tipwindow = tw = Toplevel(self.widget)
30 tw.wm_overrideredirect(1)
31 tw.wm_geometry("+%d+%d" % (x, y))
Tony Lownds8b1b8d62002-09-23 01:04:05 +000032 try:
33 # This command is only needed and available on Tk >= 8.4.0 for OSX
34 # Without it, call tips intrude on the typing process by grabbing
35 # the focus.
36 tw.tk.call("::tk::unsupported::MacWindowStyle", "style", tw._w,
37 "help", "noActivates")
38 except TclError:
39 pass
David Scherer7aced172000-08-15 01:13:23 +000040 label = Label(tw, text=self.text, justify=LEFT,
41 background="#ffffe0", relief=SOLID, borderwidth=1,
42 font = self.widget['font'])
43 label.pack()
Kurt B. Kaiser9a1ae1a2001-07-12 22:26:44 +000044
David Scherer7aced172000-08-15 01:13:23 +000045 def hidetip(self):
46 tw = self.tipwindow
47 self.tipwindow = None
48 if tw:
49 tw.destroy()
50
51
52###############################
53#
54# Test Code
55#
56class container: # Conceptually an editor_window
57 def __init__(self):
58 root = Tk()
59 text = self.text = Text(root)
60 text.pack(side=LEFT, fill=BOTH, expand=1)
61 text.insert("insert", "string.split")
62 root.update()
63 self.calltip = CallTip(text)
64
65 text.event_add("<<calltip-show>>", "(")
66 text.event_add("<<calltip-hide>>", ")")
67 text.bind("<<calltip-show>>", self.calltip_show)
68 text.bind("<<calltip-hide>>", self.calltip_hide)
Kurt B. Kaiser9a1ae1a2001-07-12 22:26:44 +000069
David Scherer7aced172000-08-15 01:13:23 +000070 text.focus_set()
71 # root.mainloop() # not in idle
72
73 def calltip_show(self, event):
74 self.calltip.showtip("Hello world")
75
76 def calltip_hide(self, event):
77 self.calltip.hidetip()
78
79def main():
80 # Test code
81 c=container()
82
83if __name__=='__main__':
84 main()