Ejemplo n.º 1
0
class CreateToolTip(object):
    """
    create a tooltip for a given widget
    """
    def __init__(self, widget, text='widget info'):
        self.waittime = 500  # miliseconds
        self.wraplength = 180  # pixels
        self.widget = widget
        self.text = text
        self.widget.bind("<Enter>", self.enter)
        self.widget.bind("<Leave>", self.leave)
        self.widget.bind("<ButtonPress>", self.leave)
        self.id = None
        self.tw = None

    def enter(self, event=None):
        self.schedule()

    def leave(self, event=None):
        self.unschedule()
        self.hidetip()

    def schedule(self):
        self.unschedule()
        self.id = self.widget.after(self.waittime, self.showtip)

    def unschedule(self):
        id = self.id
        self.id = None
        if id:
            self.widget.after_cancel(id)

    def showtip(self, event=None):
        x = y = 0
        x, y, cx, cy = self.widget.bbox("insert")
        x += self.widget.winfo_rootx() + 25
        y += self.widget.winfo_rooty() + 20
        # creates a toplevel window
        self.tw = Toplevel(self.widget)
        # Leaves only the label and removes the app window
        self.tw.wm_overrideredirect(True)
        self.tw.wm_geometry("+%d+%d" % (x, y))
        label = Label(self.tw,
                      text=self.text,
                      justify='left',
                      background="#ffffff",
                      relief='solid',
                      borderwidth=0,
                      wraplength=self.wraplength)
        label.pack(ipadx=1)

    def hidetip(self):
        tw = self.tw
        self.tw = None
        if tw:
            tw.destroy()
Ejemplo n.º 2
0
class Tooltip_Helper:
    """
  Class to help show tooltips on Tkinter Canvases.
  """
    def __init__(self, canvas):
        """
    |canvas|: the canvas on which tooltips will be shown.
    """
        self._canvas = canvas
        self._tip = None

    def show_tooltip(self, x, y, text, background=TOOLTIP_BACKGROUND):
        """
    Shows a tooltip containing the given |text| close to the point (|x|, |y|) on
        the canvas.
    """
        x += self._canvas.winfo_rootx() + 10
        y += self._canvas.winfo_rooty() + 10
        geometry = '+%d+%d' % (x, y)
        if not self._tip:
            self._tip = Toplevel(self._canvas)
            self._tip.wm_overrideredirect(1)
            self._tip.wm_geometry(geometry)
            try:
                # for Mac OS
                self._tip.tk.call('::tk::unsupported::MacWindowStyle', 'style',
                                  self._tip._w, 'help', 'noActivates')
            except TclError:
                pass
            self.label = Label(self._tip,
                               text=text,
                               justify=LEFT,
                               background=background,
                               relief=SOLID,
                               borderwidth=1,
                               font=DEFAULT_FONT)
            self.label.pack()
        else:
            self._tip.geometry(geometry)
            self.label.config(text=text, background=background)

    def hide_tooltip(self):
        """
    Hides the previously added tooltip, if any.
    """
        if self._tip:
            self._tip.destroy()
            self._tip = None
Ejemplo n.º 3
0
class Tooltip_Helper:
  """
  Class to help show tooltips on Tkinter Canvases.
  """
  def __init__(self, canvas):
    """
    |canvas|: the canvas on which tooltips will be shown.
    """
    self._canvas = canvas
    self._tip = None
  def show_tooltip(self, x, y, text, background=TOOLTIP_BACKGROUND):
    """
    Shows a tooltip containing the given |text| close to the point (|x|, |y|) on
        the canvas.
    """
    x += self._canvas.winfo_rootx() + 10
    y += self._canvas.winfo_rooty() + 10
    geometry = '+%d+%d' % (x, y)
    if not self._tip:
      self._tip = Toplevel(self._canvas)
      self._tip.wm_overrideredirect(1)
      self._tip.wm_geometry(geometry)
      try:
        # for Mac OS
        self._tip.tk.call('::tk::unsupported::MacWindowStyle', 'style',
            self._tip._w, 'help', 'noActivates')
      except TclError:
        pass
      self.label = Label(self._tip, text=text, justify=LEFT,
          background=background, relief=SOLID, borderwidth=1, font=DEFAULT_FONT)
      self.label.pack()
    else:
      self._tip.geometry(geometry)
      self.label.config(text=text, background=background)
  def hide_tooltip(self):
    """
    Hides the previously added tooltip, if any.
    """
    if self._tip:
      self._tip.destroy()
      self._tip = None