Display tooltip when hovering on a tab

Viewed 744

I'm using DynamicHelp to display tooltips. The problem is that it only displays help when the cursor is on the body of the tab: not when it is on the tab itself. What I'd like to happen is for the help text to be displayed when the user is hovering over the tabs instead of having to select the tab, then move the cursor to the body before the help is displayed.

package require BWidget

 ## create a notebook with 2 text panes
 NoteBook .n
 .n insert 0 tb1 -text "Tab 1"
 .n insert 1 tb2 -text "Tab 2"
 foreach panel {tb1 tb2} {
    set pane [.n getframe $panel]
    text $pane.t
    pack $pane.t -fill both -expand 1
 }
 pack .n
 .n raise tb1

#                    ,-- How do I get the tab?
DynamicHelp::add [.n getframe tb1] -text "The essence of silly\nsally silica"
DynamicHelp::add [.n getframe tb2] -text "acetyl sali cylic\nacid is aspirin"

I found this piece of code on the notebook implementation - I don't know if it helps. I can't figure out how it gets the handle of the tab from this.

proc NoteBook::_highlight { type path page } {
    variable $path
    upvar 0  $path data

    if { [string equal [Widget::cget $path.f$page -state] "disabled"] } {
        return
    }

    switch -- $type {
        on {
            $path.c itemconfigure "$page:poly" \
            -fill [_getoption $path $page -activebackground]
            $path.c itemconfigure "$page:text" \
            -fill [_getoption $path $page -activeforeground]
        }
        off {
            $path.c itemconfigure "$page:poly" \
            -fill [_getoption $path $page -background]
            $path.c itemconfigure "$page:text" \
            -fill [_getoption $path $page -foreground]
        }
    }
}
3 Answers

I have written a small extension to the Notebook widget what does exactly what you want. You can download it from notebook-tip.tcl. Use it as follows:

After package require, source this file. Create your tabs and add the balloons. Multiple lines are possible.

Example:

package require BWidget
source notebook-tip.tcl

NoteBook .n
.n insert 0 tb1 -text "Tab 1"
.n balloon tb1 "balloon text for Tab 1"
.n insert 1 tb2 -text "Tab 2"
.n balloon tb2 "balloon text for Tab 2"
foreach panel {tb1 tb2} {
  # add contents
  set pane [.n getframe $panel]
  text $pane.t
  pack $pane.t -fill both -expand 1
}
.n raise tb1
grid .n -sticky ew

You can change the balloon text dynamically with itemconfigure:

$path itemconfigure $page -balloon text

For example:

.n itemconfigure tb1 -balloon "another text"

Not quite the solution I was looking for but it is good enough. Create a label for the help text and bind the entry of the tab to the label

package require BWidget

# Creat a bar for help
grid [label .l1 -textvariable tabhelp -justify left] -sticky w -row 0
## create a notebook with 2 text panes
NoteBook .n
.n insert 0 tb1 -text "Tab 1"
.n insert 1 tb2 -text "Tab 2"
foreach panel {tb1 tb2} {
   set pane [.n getframe $panel]
   text $pane.t
   pack $pane.t -fill both -expand 1
}
.n raise tb1
grid .n -sticky ew -row 1

DynamicHelp::add [.n getframe tb1] -text "The essence of silly\nsally silica"
DynamicHelp::add [.n getframe tb2] -text "acetyl sali cylic\nacid is aspirin"

# Add help on entry into the tabs
.n.c bind p:tb1 <Enter> {set tabhelp "Woody Woodpecker"}
.n.c bind p:tb1 <Leave> {set tabhelp ""}

.n.c bind p:tb2 <Enter> {set tabhelp "Aspirins are great"}
.n.c bind p:tb2 <Leave> {set tabhelp ""}

Really, you can. You must add the the option -helptext in the command "insert".

According to Bwidget doc :

[...]

pathName insert index page ?option value...?

Insert a new page identified by page at position index in the pages list. index must be numeric or end. The pathname of the new page is returned. Dynamic help, if it is specified by the options, is displayed when the pointer hovers over the tab that belongs to the page.

-helpcmd
    Has no effect. See also DynamicHelp. 

-helptext
    Text for dynamic help. If empty, no help is available for this page. See also DynamicHelp. 

-helptype
    Type of dynamic help. Use balloon (the default for a NoteBook page) or variable. See also DynamicHelp. 

-helpvar
    Variable to use when -helptype option is variable. See also DynamicHelp. 

[...]

Related