Updated 2009-11-21 12:14:22 by CME

"Tooltips" are also known as "balloon help". Search [1] for "tooltips" in the "Bag of Tk algorithms", "Example Scripts Everybody Should Have", ...

Such extensions as ClassyTcl, ...., the Perl/Tk Tk::Balloon, ... build-in tooltips as programmable widgets.

I was thinking that possibly BLT had tooltips as well.

Tk::Balloon attaches balloons to canvas items, in contrast to most of the example here. However SCoTT SmeDLeY offers a pure Tk analogue [2].

Victor Wagner wrote one ...

Some shops use "tooltips" for almost any action associated with [mouseover], including ...

tklib has a tooltip package, which provides tooltip management.

Here is one tooltips implementation that I use, which was originally written by someone else and then modified by me. It´s only been tested on Windows. - [RJ 03/16/2006 I tested on Puppy Linux and Solaris 2.8 - works like a charm!]

[stabbingfinger: This is superbly simple. Fixed three problems: 1) toplevel window flashed when tooltip is displayed; 2) tooltip artifacts resulted when flying through a UI too quickly; 3) "wm attributes" assumes Windows. Additions are commented. Thanks for the code!]

[Christian Rapp: This code is great, use it very often. Fixed one thing: a.) The tooltip is not entirely visible when $pointerX + $width is greater than [winfo screenwidth .]. Placed a comment to my changes in the code.]

[Chris Edwards: Modified so that the tooltip is displayed above the pointer when in the bottom half of the screen, to avoid tooltips going offscreen when near the bottom edge (changes labelled b.). Also now has the tooltips centred horizontally on the pointer rather than being left-aligned (changes labelled c.).]
 proc setTooltip {widget text} {
        if { $text != "" } {
                # 2) Adjusted timings and added key and button bindings. These seem to
                # make artifacts tolerably rare.
                bind $widget <Any-Enter>    [list after 500 [list showTooltip %W $text]]
                bind $widget <Any-Leave>    [list after 500 [list destroy %W.tooltip]]
                bind $widget <Any-KeyPress> [list after 500 [list destroy %W.tooltip]]
                bind $widget <Any-Button>   [list after 500 [list destroy %W.tooltip]]
        }
 }
 proc showTooltip {widget text} {
        global tcl_platform
        if { [string match $widget* [winfo containing  [winfo pointerx .] [winfo pointery .]] ] == 0  } {
                return
        }

        catch { destroy $widget.tooltip }

        set scrh [winfo screenheight $widget]    ; # 1) flashing window fix
        set scrw [winfo screenwidth $widget]     ; # 1) flashing window fix
        set tooltip [toplevel $widget.tooltip -bd 1 -bg black]
        wm geometry $tooltip +$scrh+$scrw        ; # 1) flashing window fix
        wm overrideredirect $tooltip 1

        if {$tcl_platform(platform) == {windows}} { ; # 3) wm attributes...
                wm attributes $tooltip -topmost 1   ; # 3) assumes...
        }                                           ; # 3) Windows
        pack [label $tooltip.label -bg lightyellow -fg black -text $text -justify left]

        set width [winfo reqwidth $tooltip.label]
        set height [winfo reqheight $tooltip.label]

        set pointer_below_midline [expr [winfo pointery .] > [expr [winfo screenheight .] / 2.0]]                ; # b.) Is the pointer in the bottom half of the screen?

        set positionX [expr [winfo pointerx .] - round($width / 2.0)]    ; # c.) Tooltip is centred horizontally on pointer.
        set positionY [expr [winfo pointery .] + 35 * ($pointer_below_midline * -2 + 1) - round($height / 2.0)]  ; # b.) Tooltip is displayed above or below depending on pointer Y position.

        # a.) Ad-hockery: Set positionX so the entire tooltip widget will be displayed.
        # c.) Simplified slightly and modified to handle horizontally-centred tooltips and the left screen edge.
        if  {[expr $positionX + $width] > [winfo screenwidth .]} {
                set positionX [expr [winfo screenwidth .] - $width]
        } elseif {$positionX < 0} {
                set positionX 0
        }

        wm geometry $tooltip [join  "$width x $height + $positionX + $positionY" {}]
        raise $tooltip

        # 2) Kludge: defeat rare artifact by passing mouse over a tooltip to destroy it.
        bind $widget.tooltip <Any-Enter> {destroy %W}
        bind $widget.tooltip <Any-Leave> {destroy %W}
 }

 pack [button .b -text hello]
 setTooltip .b "Hello World!"

At least two items have the name tooltips :
 What: tooltips
 Where: From the contact
 Description: Tk program which provides functionality similar to
        the one Microsoft has.
 Updated:
 Contact: mailto:pdboyer@c3i.saic.com (Paul D. Boyer)

 What: ToolTips
 Where: From the contact
 Description: Provides a way to allow one to associate Microsoft-like tooltips
        to icons in a Tk application.
 Updated:
 Contact: mailto:pdboyer@c3i.saic.com (Paul Boyer )

Other packages and applications relating to tooltips or balloon help include:

 What: tktray
 Where: http://www.electricmemo.com/tktray.zip ???
 Description: Simple application running as a toolbar app under
        Windows 95/NT. Set the tooltip text, icons, etc.
        Written in Delphi 2.
 Updated: 06/1998
 Contact: mailto:markl@electricmemo.com (Mark Lawson)

  • [XWordPad]
 What: XWordPad
 Where: http://www.geocities.com/SiliconValley/Campus/6846/
 Description: Tcl/Tk text editor supporting plain text of unlimited
        size.  Uses standard menus, supports copy, paste, cut,
        unlimited undo/redo, toolbar and balloon help, quick
        search, font, style and size choices, multifile file
        editing, etc.
        Runs on Tcl/Tk 7.4-8.x, and works on Unix and Windows.
        Currently at version 2.1.
 Updated: 02/2000
 Contact: mailto:andrew.lee@iname.com

 What: ClassyTcl/Tk
 Where: http://rrna.uia.ac.be/classytcl/
 Description: Dynamically loadable object system (both tcl and C based are
        available), widget set and GUI builder.
        ClassyTk is a widget set which includes BarChart, ChartGrid,
        LineChart, Balloon (help),
        CmdWidget (command line widget), DefaultMenu (select from
        user defined defaults), DragDrop, DynaMenu, DynaTool (toolbar),
        FileSelect, InputDialog, ProgressDialog, SaveDialog, SelectDialog,
        getcolor, getfont, yorn, Browser, Editor, Fold (foldable frame),
        HTML, MultiFrame, NoteBook, OptionBox, OptionMenu, Paned, Progress,
        Table, Tree, ColorEntry, FontSelect, Selector, as well as improved
        versions of Canvas (supports zooming, undo/redo, rotate, save, load,
        group, and printing), Entry, FileEntry, ListBox, Message, NumEntry,
        RepeatButton, ScrolledFrame, Text, and more.
        The ClassyTcl Builder can be used to graphically create interfaces,
        and is invoked via the cbuild command.
        Comes with sample applications such as cedit,
        ccalc (calculator),
        ccenter (early stage program manager),
        cdraw (vector drawing),
        cedit (text editor),
        cfiles (early stage file manager),
        ctester (test ClassyTcl widgets).
        Requires Tcl/Tk 8.x and ExtraL if ClassyTcl widgets are used.
        Currently at ClassyTcl and ClassyTk are at version 1.0.0 .
 Updated: 08/2001
 Contact: mailto:derijkp@uia.ua.ac.be (Peter.DeRijk)

 What: MegaWidget package
 Where: http://www.purl.org/net/hobbs/tcl/script/widget/widget-0.9.tar.gz
 Description: Tk 8.x megawidget capability.
        While tested thru Tk 8.0b2, some problems still remained
        under Windows with Tk 8.
        Megawidgets included in this package are combobox, console,
        hierarchy list display, progressbar, tabnotebook, validating
        entry widget, and scrolledtext.  Also, support for balloon help
        and paned window management are included.
 Updated: 03/2001
 Contact: mailto:jeff@hobbs.org

 What: Miscellaneous Tcl procs (Corey)
 Where: http://www.kencorey.com/tcl/answers.html
 Description: Examples of how to do things like provide balloon help over menu
        items, do background processing, eat events while a program is busy,
        do combobox widgets, scroll two text widgets with one scrollbar,
        scroll to currently focused canvas item, moving graphs, move
        the nodes of a polygon around dynamically, track what procedures
        are called (with what arguments), scrolling a grid managed frame in
        a canvas, using fileevent on a pipe, simple spreadsheet using
        grid, and a simple listbox inside a text widget.
        The contact isn't currently updating this page.
 Updated: 08/1998
 Contact: mailto:ken@kencorey.com (Ken Corey)

 What: tkballoon
 Where: http://www.multimania.com/droche/tkballoon/
 Description: Pure Tcl/Tk implementation of balloon help.
 Updated: 07/1999
 Contact: mailto:daniel.roche@bigfoot.com (Daniel Roche)

  • ]Win32 Tcl and Tk patches (jessikat)]
 What: Win32 Tcl and Tk patches (jessikat)
 Where: http://www.jessikat.demon.co.uk/
 Description: A variety of patches to Tcl and Tk for Win32s.
        There are patches for wm activate bug, memory debug support,
        listbox justify, cursor, panics, and tkCanvWind.  Also on this
        page are patches and ports of a variety of extensions and
        applications such as DDE, tclStruct, BLT, otcl and a clock
        application.  Tiled widgets problematic, bgexec/busy not
        working, drag&drop ok with dde.  Also a small balloonHelp (blnhlp)
        package is available here as well.  Rotating text didn't make
        it, nor did drag and drop.  Also find a general dll
        caller for the Intel win32 platform which creates a namespace
        called dll containing the dll routines that can be called.
        Contact tested with VC++ 6.0 and Tcl 8.0.4.  It relies on the
        way MSC 4.0.  Site also has a crypt dll.
 Updated: 04/1998
 Contact: mailto:robin@jessikat.demon.co.uk

 What: pTk Rezic widget collection
 Where: http://user.cs.tu-berlin.de/%7Eeserte/src/perl/
        http://www.cpan.org/
 Description: Tk::HistEntry provides an Entry widget with a history.
        Tk::Date is a date widget in development.
        On CPAN, you can find Tk::Getopt (Tk-Options?), which is a
        GUI interface for Perl/Tk with interface to Getopt::Long.
        From the user, you may be able to get the following.
        Tk::UnderlineAll adds accellerators
        to menu buttons, entries and notebook pages. Tk::WListbox
        adds enhanced keybindings for selecting entries.
        Tk::ContextHelp provides context sensitive help in Perl/Tk.
        Tk::CanvasBalloon is a module for attaching help balloons to
        canvas items or tags.
        Also available are Tk::PNG and Tk::TIFF, to add support
        for those image formats.
        Tk::FBox is a perl version of the standard Tk filebox.
        Tie-Listbox is an experimental module using ties to Tk listboxes.
        FlatCheckbox is a canvas based checkbox.
 Updated: 05/1999
 Contact: mailto:eserte@cs.tu-berlin.de (Slaven Rezic)

Larry Smith weedesk has a simple balloon help system. The code:
 label .help -bd 1 -fg black -bg lightyellow -font fixed -text "default help"

 set btn3 0
 proc popballoon {} {
   global btn3
   if { !$btn3 } { place forget .help }
 }

 proc help {w help} {
   bind $w <Any-Enter> "after 1000 balloon %W $help; after 3000 popballoon"
   bind $w <Any-Leave> "popballoon"
   bind $w <ButtonPress-3> "set btn3 1; balloon %W $help"
   bind $w <ButtonRelease-3> "set btn3 0; popballoon"
 }

 proc balloon { w args } {
   .help configure -text $args
   regexp {^\.[A-Za-z0-9]*} $w parent
   if { [ catch { set x [ expr [ winfo x $w ] + [ winfo x $parent ] + 10 ] } ] == 0 } {
     set y [ expr [ winfo y $w ] + [ winfo y $parent ] - 10 ]
     place .help -x $x -y $y
     raise .help
   }
 }

To install a tooltip on a particular widget:
 help $button_name $helptext

When the mouse enters the widget the tooltip will be displayed about 1 second later and remain up for two seconds (these values are easily adjusted in the first line of the "help" proc). You can also use btn3 to display the tip as long as you like, it will disappear when you release btn3.

I was quite bored so decided to try my hand at an extensible tooltip system for Tcl using iTcl. It may not be the best, but certainly worth a look. This was my first iTcl code ever. So be kind :)

The Object:
 #===== ITcl Class ToolTip ==================================#
 # Initialize a Tool Tip object.  (Creates window if needed) #
 # Usage: ToolTip objName widgetName tipMessage              #
 # Configuration Options:                                    #
 #   tipMessage: Message to display in tooltip window        #
 #   tipDelay: Time to wait before showing/hiding tip        #
 #       Default value is 400                                #
 #   xOffset: X coordinate offset for the tootip window      #
 #   yOffset: Y coordinate offset for the tooltip window     #
 #       Default value is 20                                 #
 #===========================================================#
 itcl::class ToolTip {
    public variable widgetName ""
    public variable tipMessage ""
    public variable xOffset 20
    public variable yOffset 20
    public variable tipDelay 400
    constructor { name msg } {
        set widgetName $name
        set tipMessage $msg
        bind $name <Enter> {foreach itclTip [itcl::find objects -isa ToolTip] {if {[$itclTip cget -widgetName] == "%W"} {$itclTip show}}}
        bind $name <Leave> {foreach itclTip [itcl::find objects -isa ToolTip] {if {[$itclTip cget -widgetName] == "%W"} {$itclTip hide}}}
    }
    method show {} {
        if {![catch {toplevel .tipWindowItclClass -bg black -bd 1 -height 1 -width 1 -relief solid} blah]} {
            label .tipWindowItclClass.text -bg lightyellow -justify left -text "$tipMessage"
            grid .tipWindowItclClass.text -row 0 -column 0 -sticky w
            wm overrideredirect .tipWindowItclClass 1
            wm withdraw .tipWindowItclClass
        }
        .tipWindowItclClass.text configure -text " $tipMessage "
        wm geometry .tipWindowItclClass +[expr [winfo pointerx .] + $xOffset]+[expr [winfo pointery .] + $yOffset]
        after $tipDelay {
            wm deiconify .tipWindowItclClass
            raise .tipWindowItclClass
        }
    }
    method hide {} {
        after $tipDelay {
            wm withdraw .tipWindowItclClass
        }
    }
    destructor {
        destroy .tipWindowItclClass
    }
 }

The Sample:
  frame .top -relief flat -bd 0
  label .atext -relief flat -bd 0 -text "blah0"
  label .btext -relief flat -bd 0 -text "blah1"
  label .ctext -relief flat -bd 0 -text "blah2"
  label .dtext -relief flat -bd 0 -text "blah3"
  pack .atext -in .top -fill both
  pack .btext -in .top -fill both
  pack .ctext -in .top -fill both
  pack .dtext -in .top -fill both
  pack .top -fill both
  ToolTip #auto .atext "This is a Test"
  ToolTip #auto .btext "This too"
  ToolTip #auto .ctext "Yup again"
  ToolTip #auto .dtext "It's over now"

* Submitted by: Barry Skidmore *

See also: