0001 """A CallTip window class for Tkinter/IDLE. 0002 0003 After ToolTip.py, which uses ideas gleaned from PySol 0004 Used by the CallTips IDLE extension. 0005 0006 """ 0007 from Tkinter import * 0008 0009 class CallTip: 0010 0011 def __init__(self, widget): 0012 self.widget = widget 0013 self.tipwindow = None 0014 self.id = None 0015 self.x = self.y = 0 0016 0017 def showtip(self, text): 0018 " Display text in calltip window" 0019 # truncate overly long calltip 0020 if len(text) >= 79: 0021 text = text[:75] + ' ...' 0022 self.text = text 0023 if self.tipwindow or not self.text: 0024 return 0025 self.widget.see("insert") 0026 x, y, cx, cy = self.widget.bbox("insert") 0027 x = x + self.widget.winfo_rootx() + 2 0028 y = y + cy + self.widget.winfo_rooty() 0029 self.tipwindow = tw = Toplevel(self.widget) 0030 # XXX 12 Dec 2002 KBK The following command has two effects: It removes 0031 # the calltip window border (good) but also causes (at least on 0032 # Linux) the calltip to show as a top level window, burning through 0033 # any other window dragged over it. Also, shows on all viewports! 0034 tw.wm_overrideredirect(1) 0035 tw.wm_geometry("+%d+%d" % (x, y)) 0036 try: 0037 # This command is only needed and available on Tk >= 8.4.0 for OSX 0038 # Without it, call tips intrude on the typing process by grabbing 0039 # the focus. 0040 tw.tk.call("::tk::unsupported::MacWindowStyle", "style", tw._w, 0041 "help", "noActivates") 0042 except TclError: 0043 pass 0044 label = Label(tw, text=self.text, justify=LEFT, 0045 background="#ffffe0", relief=SOLID, borderwidth=1, 0046 font = self.widget['font']) 0047 label.pack() 0048 0049 def hidetip(self): 0050 tw = self.tipwindow 0051 self.tipwindow = None 0052 if tw: 0053 tw.destroy() 0054 0055 0056 ############################### 0057 # 0058 # Test Code 0059 # 0060 class container: # Conceptually an editor_window 0061 def __init__(self): 0062 root = Tk() 0063 text = self.text = Text(root) 0064 text.pack(side=LEFT, fill=BOTH, expand=1) 0065 text.insert("insert", "string.split") 0066 root.update() 0067 self.calltip = CallTip(text) 0068 0069 text.event_add("<<calltip-show>>", "(") 0070 text.event_add("<<calltip-hide>>", ")") 0071 text.bind("<<calltip-show>>", self.calltip_show) 0072 text.bind("<<calltip-hide>>", self.calltip_hide) 0073 0074 text.focus_set() 0075 root.mainloop() 0076 0077 def calltip_show(self, event): 0078 self.calltip.showtip("Hello world") 0079 0080 def calltip_hide(self, event): 0081 self.calltip.hidetip() 0082 0083 def main(): 0084 # Test code 0085 c=container() 0086 0087 if __name__=='__main__': 0088 main() 0089
Generated by PyXR 0.9.4