blob: 990d96eedb00e071aa97cf7c0db9b03a8440e6c4 [file] [log] [blame]
Kurt B. Kaisere54710b2002-12-12 19:15:39 +00001"""A CallTip window class for Tkinter/IDLE.
David Scherer7aced172000-08-15 01:13:23 +00002
Kurt B. Kaisere54710b2002-12-12 19:15:39 +00003After ToolTip.py, which uses ideas gleaned from PySol
4Used by the CallTips IDLE extension.
5
6"""
David Scherer7aced172000-08-15 01:13:23 +00007from Tkinter import *
8
9class CallTip:
10
11 def __init__(self, widget):
12 self.widget = widget
13 self.tipwindow = None
14 self.id = None
15 self.x = self.y = 0
16
17 def showtip(self, text):
Kurt B. Kaisere54710b2002-12-12 19:15:39 +000018 " Display text in calltip window"
19 # truncate overly long calltip
Kurt B. Kaisere72f05d2002-09-15 21:43:13 +000020 if len(text) >= 79:
21 text = text[:75] + ' ...'
David Scherer7aced172000-08-15 01:13:23 +000022 self.text = text
23 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)
Kurt B. Kaisere54710b2002-12-12 19:15:39 +000030 # XXX 12 Dec 2002 KBK The following command has two effects: It removes
31 # the calltip window border (good) but also causes (at least on
32 # Linux) the calltip to show as a top level window, burning through
33 # any other window dragged over it. Also, shows on all viewports!
David Scherer7aced172000-08-15 01:13:23 +000034 tw.wm_overrideredirect(1)
35 tw.wm_geometry("+%d+%d" % (x, y))
Tony Lownds8b1b8d62002-09-23 01:04:05 +000036 try:
37 # This command is only needed and available on Tk >= 8.4.0 for OSX
38 # Without it, call tips intrude on the typing process by grabbing
39 # the focus.
Kurt B. Kaiser6655e4b2002-12-31 16:03:23 +000040 tw.tk.call("::tk::unsupported::MacWindowStyle", "style", tw._w,
Tony Lownds8b1b8d62002-09-23 01:04:05 +000041 "help", "noActivates")
42 except TclError:
43 pass
David Scherer7aced172000-08-15 01:13:23 +000044 label = Label(tw, text=self.text, justify=LEFT,
45 background="#ffffe0", relief=SOLID, borderwidth=1,
46 font = self.widget['font'])
47 label.pack()
Kurt B. Kaiser9a1ae1a2001-07-12 22:26:44 +000048
David Scherer7aced172000-08-15 01:13:23 +000049 def hidetip(self):
50 tw = self.tipwindow
51 self.tipwindow = None
52 if tw:
53 tw.destroy()
54
55
56###############################
57#
58# Test Code
59#
60class container: # Conceptually an editor_window
61 def __init__(self):
62 root = Tk()
63 text = self.text = Text(root)
64 text.pack(side=LEFT, fill=BOTH, expand=1)
65 text.insert("insert", "string.split")
66 root.update()
67 self.calltip = CallTip(text)
68
69 text.event_add("<<calltip-show>>", "(")
70 text.event_add("<<calltip-hide>>", ")")
71 text.bind("<<calltip-show>>", self.calltip_show)
72 text.bind("<<calltip-hide>>", self.calltip_hide)
Kurt B. Kaiser9a1ae1a2001-07-12 22:26:44 +000073
David Scherer7aced172000-08-15 01:13:23 +000074 text.focus_set()
Kurt B. Kaisere54710b2002-12-12 19:15:39 +000075 root.mainloop()
David Scherer7aced172000-08-15 01:13:23 +000076
77 def calltip_show(self, event):
78 self.calltip.showtip("Hello world")
79
80 def calltip_hide(self, event):
81 self.calltip.hidetip()
82
83def main():
84 # Test code
85 c=container()
86
87if __name__=='__main__':
88 main()