jpayne@69: """Tools for displaying tool-tips. jpayne@69: jpayne@69: This includes: jpayne@69: * an abstract base-class for different kinds of tooltips jpayne@69: * a simple text-only Tooltip class jpayne@69: """ jpayne@69: from tkinter import * jpayne@69: jpayne@69: jpayne@69: class TooltipBase(object): jpayne@69: """abstract base class for tooltips""" jpayne@69: jpayne@69: def __init__(self, anchor_widget): jpayne@69: """Create a tooltip. jpayne@69: jpayne@69: anchor_widget: the widget next to which the tooltip will be shown jpayne@69: jpayne@69: Note that a widget will only be shown when showtip() is called. jpayne@69: """ jpayne@69: self.anchor_widget = anchor_widget jpayne@69: self.tipwindow = None jpayne@69: jpayne@69: def __del__(self): jpayne@69: self.hidetip() jpayne@69: jpayne@69: def showtip(self): jpayne@69: """display the tooltip""" jpayne@69: if self.tipwindow: jpayne@69: return jpayne@69: self.tipwindow = tw = Toplevel(self.anchor_widget) jpayne@69: # show no border on the top level window jpayne@69: tw.wm_overrideredirect(1) jpayne@69: try: jpayne@69: # This command is only needed and available on Tk >= 8.4.0 for OSX. jpayne@69: # Without it, call tips intrude on the typing process by grabbing jpayne@69: # the focus. jpayne@69: tw.tk.call("::tk::unsupported::MacWindowStyle", "style", tw._w, jpayne@69: "help", "noActivates") jpayne@69: except TclError: jpayne@69: pass jpayne@69: jpayne@69: self.position_window() jpayne@69: self.showcontents() jpayne@69: self.tipwindow.update_idletasks() # Needed on MacOS -- see #34275. jpayne@69: self.tipwindow.lift() # work around bug in Tk 8.5.18+ (issue #24570) jpayne@69: jpayne@69: def position_window(self): jpayne@69: """(re)-set the tooltip's screen position""" jpayne@69: x, y = self.get_position() jpayne@69: root_x = self.anchor_widget.winfo_rootx() + x jpayne@69: root_y = self.anchor_widget.winfo_rooty() + y jpayne@69: self.tipwindow.wm_geometry("+%d+%d" % (root_x, root_y)) jpayne@69: jpayne@69: def get_position(self): jpayne@69: """choose a screen position for the tooltip""" jpayne@69: # The tip window must be completely outside the anchor widget; jpayne@69: # otherwise when the mouse enters the tip window we get jpayne@69: # a leave event and it disappears, and then we get an enter jpayne@69: # event and it reappears, and so on forever :-( jpayne@69: # jpayne@69: # Note: This is a simplistic implementation; sub-classes will likely jpayne@69: # want to override this. jpayne@69: return 20, self.anchor_widget.winfo_height() + 1 jpayne@69: jpayne@69: def showcontents(self): jpayne@69: """content display hook for sub-classes""" jpayne@69: # See ToolTip for an example jpayne@69: raise NotImplementedError jpayne@69: jpayne@69: def hidetip(self): jpayne@69: """hide the tooltip""" jpayne@69: # Note: This is called by __del__, so careful when overriding/extending jpayne@69: tw = self.tipwindow jpayne@69: self.tipwindow = None jpayne@69: if tw: jpayne@69: try: jpayne@69: tw.destroy() jpayne@69: except TclError: # pragma: no cover jpayne@69: pass jpayne@69: jpayne@69: jpayne@69: class OnHoverTooltipBase(TooltipBase): jpayne@69: """abstract base class for tooltips, with delayed on-hover display""" jpayne@69: jpayne@69: def __init__(self, anchor_widget, hover_delay=1000): jpayne@69: """Create a tooltip with a mouse hover delay. jpayne@69: jpayne@69: anchor_widget: the widget next to which the tooltip will be shown jpayne@69: hover_delay: time to delay before showing the tooltip, in milliseconds jpayne@69: jpayne@69: Note that a widget will only be shown when showtip() is called, jpayne@69: e.g. after hovering over the anchor widget with the mouse for enough jpayne@69: time. jpayne@69: """ jpayne@69: super(OnHoverTooltipBase, self).__init__(anchor_widget) jpayne@69: self.hover_delay = hover_delay jpayne@69: jpayne@69: self._after_id = None jpayne@69: self._id1 = self.anchor_widget.bind("", self._show_event) jpayne@69: self._id2 = self.anchor_widget.bind("", self._hide_event) jpayne@69: self._id3 = self.anchor_widget.bind("