blob: 24af6b0e46b734d8026ade37102364025944479b [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.
Guido van Rossum85b97351999-06-02 16:10:19 +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):
Tim Peters32b069c2002-04-22 18:43:49 +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] + ' ...'
Guido van Rossum85b97351999-06-02 16:10:19 +000021 self.text = text
Tim Peters32b069c2002-04-22 18:43:49 +000022
Guido van Rossum85b97351999-06-02 16:10:19 +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))
32 label = Label(tw, text=self.text, justify=LEFT,
Guido van Rossumab3b50b1999-06-10 14:19:46 +000033 background="#ffffe0", relief=SOLID, borderwidth=1,
34 font = self.widget['font'])
Guido van Rossum85b97351999-06-02 16:10:19 +000035 label.pack()
Tim Peters70c43782001-01-17 08:48:39 +000036
Guido van Rossum85b97351999-06-02 16:10:19 +000037 def hidetip(self):
38 tw = self.tipwindow
39 self.tipwindow = None
40 if tw:
41 tw.destroy()
42
43
44###############################
45#
46# Test Code
47#
48class container: # Conceptually an editor_window
49 def __init__(self):
50 root = Tk()
51 text = self.text = Text(root)
52 text.pack(side=LEFT, fill=BOTH, expand=1)
53 text.insert("insert", "string.split")
54 root.update()
55 self.calltip = CallTip(text)
56
57 text.event_add("<<calltip-show>>", "(")
58 text.event_add("<<calltip-hide>>", ")")
59 text.bind("<<calltip-show>>", self.calltip_show)
60 text.bind("<<calltip-hide>>", self.calltip_hide)
Tim Peters70c43782001-01-17 08:48:39 +000061
Guido van Rossum85b97351999-06-02 16:10:19 +000062 text.focus_set()
63 # root.mainloop() # not in idle
64
65 def calltip_show(self, event):
66 self.calltip.showtip("Hello world")
67
68 def calltip_hide(self, event):
69 self.calltip.hidetip()
70
71def main():
72 # Test code
73 c=container()
74
75if __name__=='__main__':
76 main()