Getting started with BWidget

A BWidget is a megawidget written in pure Tcl/Tk. See the BWidget page for a listing of all available widgets.

Installing and Initial Checking

BWidget is available as a part of the ActiveTCL Tcl/Tk distribution from ActiveState. It is also available as an installable package for most popular linux distributions. Because BWidget is pure Tcl/Tk, it is also possible to download and install it in a suitable location. The URL for download is given against "Where:" on BWidget page. If you download BWidget yourself, install it in a location where it can be found by the auto_path. If you want to install BWidget in a non-standard location, you must then manually extend the auto_path to include that location. Tips for checking and updating auto_path are given further below.

BWidget is available in some Linux distributions' package repositories. On Fedora 21 you can install it with sudo yum install bwidget.

To check whether BWidget is installed in a location known to your Tcl/Tk, start a tclsh interactive session and issue a 'package require' command as follows:

 $> tclsh
 % package require BWidget
 1.7

In the example session shown above, BWidget version 1.7 was found.

If the result is

 can't find package BWidget

then most likely BWidget is not installed or the auto_path is not properly set.

Checking auto_path:

 % set auto_path
 /usr/lib/tcl8.4 /usr/lib /usr/lib/tcllib1.6 /usr/lib/tk8.4

Sometimes BWidget must be manually installed in some non-standard location because of lack of write permission to these directories. Let's say we had to place it under $HOME/local/lib/tcl/bwidget-1.7.0. Then:

 % lappend auto_path $env(HOME)/local/lib/tcl
 /usr/lib/tcl8.4 /usr/lib /usr/lib/tcllib1.6 /usr/lib/tk8.4 /home/n00b3/local/lib/tcl
 % package require BWidget
 1.7

The package management system in tcl does not care that the directory where BWidget resides is called bwidget-1.7.0, or something else! The important thing is the pkgIndex file inside the bwidget-1.7.0 directory, but pkgIndex is out of scope for now. Because Tcl does not care much about the name of the directory, it is possible to have more versions of BWidget located in the /home/n00b3/local/lib/tcl directory. This way it is possible to fetch a new version from the web, put it in a local directory, set the auto_path and then start using BWidget.

First steps, using the Tree Widget

There are many widgets in BWidget and one of the most interesting perhaps is the BWidget::Tree because core Tk does not provide a tree widget. Many types of browsers use a tree to show hierarchical relations.

From now on BWidget must be successfuly loaded with the package require BWidget command. Cut and paste into a tclsh interactive session or in tkcon

Creating a small tree:

 package require BWidget
 Tree .t ;# Because of the way BWidget is written it is not BWidget::Tree
 pack .t
 .t insert end root   fruit      -text fruit
 .t insert end fruit  apple      -text apple
 .t insert end fruit  orange     -text orange
 .t insert end fruit  peach      -text peach
 .t insert end fruit  grape      -text grape 
 .t insert end root   cake       -text cake
 .t insert end cake   cheese     -text cheese
 .t insert end cake   cream      -text cream
 .t insert end cake   strawberry -text strawberry
 .t insert end root   drinks     -text drinks
 .t insert end drinks coffee     -text coffee
 .t insert end drinks tea        -text tea
 .t insert end drinks beer       -text beer
 .t insert end drinks water      -text water

As in most other tree widget implementations clicking on the '+' before the node opens the node, and pressing the '-' closes the node. If all tree nodes are visible, some of the nodes dissapear underneath the window. Resizing the window would be one option, but so far the size of the tree widget will stay the same.

Example using ScrolledWindow and Tree

A better way is to use another BWidget called BWidget::ScrolledWindow. A ScrolledWindow will apply scrollbars horizontally and vertically when needed:

 package require BWidget
 ScrolledWindow .sw
 pack .sw
 Tree .sw.t
 pack .sw.t
 .sw setwidget .sw.t   ;# Make ScrolledWindow manage the Tree widget
 update                ;# Process all UI events before moving on.

 .sw.t insert end root   fruit      -text fruit
 .sw.t insert end fruit  apple      -text apple
 .sw.t insert end fruit  orange     -text orange
 .sw.t insert end fruit  peach      -text peach
 .sw.t insert end fruit  grape      -text grape 
 .sw.t insert end root   cake       -text cake
 .sw.t insert end cake   cheese     -text cheese
 .sw.t insert end cake   cream      -text cream
 .sw.t insert end cake   strawberry -text strawberry
 .sw.t insert end root   drinks     -text drinks
 .sw.t insert end drinks coffee     -text coffee
 .sw.t insert end drinks tea        -text tea
 .sw.t insert end drinks beer       -text beer
 .sw.t insert end drinks water      -text water

If the

 update

above is not included, then an annoying vertical scrollbar is visible, at least on my machine. The update force Tk to process all remaining events in drawing the user interface before moving on.

It is possible to bind actions to a tree:

 proc node_puts {args} {
     puts $args
 }
 .sw.t bindText <1> +node_puts

The '+' in front of node_puts is important in any Tk event binding as it cause the node_puts procedure to be added to processes already bound to a widget. If it is not included (try it out) the selection background will not move as other nodes are clicked.

The procedure node_puts is just printing the name of the node selected in the tree to stdout. When the script is called by the core, the name of the node selected is appended to the script bound to the action <1> which is a Button-1 event. This is a common way to create callbacks in Tk.


philc - 2009-08-17 08:38:23

Dear all,

I would like to add the fact that BWidget framework has been originally developed by Unifix, the company I created in the 90'. It would be nice to see a short reference. Thanks.

Philippe Chassany

JSB Philippe if you have a blurb about it, I would put it here BWidget


pLasma - 2009-10-15 14:33:48

Hello there. I am having a problem with inserting image icons(the get Folder and get File ones) in a Tree widget. The icons get inserted fine, but the icons overlap the text of the nodes:

http://i33.tinypic.com/359jz2x.jpg

Any ideas on this?

JOB Please try the -padx option just like:

package require BWidget

set t [Tree .t -deltax 15 -deltay 15 -padx 25]
  pack $t

$t insert end root 0 -text "hi" -image [Bitmap::get Folder]
$t insert end root 1 -text "hello there" -image [Bitmap::get Folder]

nick - 2009-11-05 07:52:30

I have to check if a selected node in the tree is a leaf node or not.Currently I am using the following method:

set sel [$tr selection get] #get the selected node
set leaf_chk [$tr nodes $sel] #check if there are child nodes for the selected node
if {$leaf_chk == ""} { #node is a leaf node}

Is there a simpler way?


Bfox - 2010-03-03 09:56:14

Including BWidget in tk8.4 pkgIndex

Is there anyway to include BWidget into a software which doesn't include BWidget in pkgIndex file / To install BWidget incase it is not installed at all? Thank you.


SanjuJoshi - 2012-10-26 10:21:16

It is as mentioned above:

Sometimes BWidget must be manually installed in some non-standard location because of lack of write permission to these directories. Let's say we had to place it under $HOME/local/lib/tcl/bwidget-1.9.6. Then:

 % lappend auto_path $env(HOME)/local/lib/tcl
 /usr/lib/tcl8.4 /usr/lib /usr/lib/tcllib1.6 /usr/lib/tk8.4 /home/n00b3/local/lib/tcl
 % package require BWidget
 1.9.6