Version 8 of poor man's progressbar

Updated 2003-08-14 08:11:09

Brett Schwarz: So, you want a progressbar, but don't want to load an external package (ok, I am digging here :) ).

Well, you can get a basic progressbar by just re-using the scale widget. Here is an example:


 scale .sc \
    -orient horizontal \
    -sliderrelief flat \
    -sliderlength 0 \
    -troughcolor #AAAAAA \
    -showvalue 0 \
    -label 0

Notice the relief is flat. You could use another relief, but the results aren't as good. We set sliderlength to 0, so initially, nothing is shown. We will take advantage of the sliderlength to actually animate the progressbar.

Here is a proc that could be used to move the progressbar:


 proc setpb {value} {
    .sc configure \
        -sliderlength [expr {$value - 3}] \
        -label $value

    return
 }

the $value - 3 is just a fudge factor to make it look nicer when display (on Linux running 8.4.4). So, an example of the usage would be:


 proc go {} {
     for {set i 5} {$i <= 80} {incr i 5} {
        setpb $i
        update idletasks
        after 50
     }
 }

 button .go -text go -command go

You can also take advantage of a 'quirk' in the scale code, and use -activebackground along with -state option to change the color of the progressbar. So, it would look like this then:


 scale .sc \
    -orient horizontal \
    -sliderrelief flat \
    -sliderlength 0 \
    -troughcolor #AAAAAA \
    -showvalue 0 \
    -label 0 \
    -activebackground blue \
    -state active

There is no option to position the counter, so you are stuck with the default. Also, if the -orient is vertical, the progressbar goes from top to bottom.

There is one catch to this last incarnation -- if the mouse moves over the slider and then back again, then the color of the slider changes to the background color of the widget.

I also noticed on Windows that when you click on the slider, the -relief changes to raised...I think this is probably a bug???

And this is what it would look like:


http://bschwarz.com/scale_gauge.jpg


PT 14-Aug-2003: The relief changing bug was fixed in 8.4.4. For the other quirks you should play with the bindings. For instance:

  bind .sc <Enter> {break}
  bind .sc <Leave> {break}
  bind .sc <Motion> {break}
  bind .sc <1> {break}
  bind .sc <ButtonRelease-1> {break}

serves to make this pretty inactive and eliminates the colour change when the mouse passes over the widget.


[ Category Example | Category GUI | Category Widget ]