[Ruby/Tk example] Feedback [incr Widget] -- take 2

This is my second take on the progress indicator example. Perhaps it's more interesting because it uses a TkTimer (aka TkAfter) to step the progress bar.

Puts up a window containing a progress bar and a button. Clicking on the button, disables it and starts stepping the progress bar. When the progress bar has filled, the button is re-enabled and the whole thing can be repeated until you're bored out of your mind :slight_smile:

<code>
#! /usr/bin/ruby -w
# Author: Morton Goldberg

路路路

#
# Date: September 6, 2006
#
# Progress Indicator 2

require 'tk'
require 'tkextlib/iwidgets'

DEBUG = []

begin
聽聽聽聽# Build a window containing a progress indicator and a button.
聽聽聽聽root = TkRoot.new {title 'Ruby/Tk Progress Indicator'}
聽聽聽聽fb = Tk::Iwidgets::Feedback.new(root) {
聽聽聽聽聽聽聽steps 20
聽聽聽聽聽聽聽labeltext "Click the Button"
聽聽聽聽聽聽聽barcolor 'red'
聽聽聽聽聽聽聽barheight 20
聽聽聽聽聽聽聽troughcolor 'gray90'
聽聽聽聽}
聽聽聽聽fb.component_widget('trough').
聽聽聽聽聽聽聽configure('relief'=>'ridge', 'borderwidth'=>4)
聽聽聽聽fb.component_widget('bar').
聽聽聽聽聽聽聽configure('relief'=>'sunken', 'borderwidth'=>5)
聽聽聽聽fb.pack('fill'=>'x', 'padx'=>15, 'pady'=>10)
聽聽聽聽btn = TkButton.new(root) {
聽聽聽聽聽聽聽text "Do Something"
聽聽聽聽聽聽聽command {btn.action}
聽聽聽聽}
聽聽聽聽btn.pack('pady'=>10)
聽聽聽聽btn.instance_variable_set(:@fb, fb)
聽聽聽聽# Starts the timer going when the button is clicked.
聽聽聽聽def btn.action
聽聽聽聽聽聽聽self.state = 'disable' # Strange -- explicit receiver required
聽聽聽聽聽聽聽# Set the timer to trigger at 200 m-sec intervals, once for each
聽聽聽聽聽聽聽# progress step.
聽聽聽聽聽聽聽$timer = TkTimer.start(200, @fb.steps) {@fb.update}
聽聽聽聽end
聽聽聽聽fb.instance_variable_set(:@btn, btn)
聽聽聽聽# Run on each timer tick.
聽聽聽聽def fb.update
聽聽聽聽聽聽聽labeltext = "Doing Something ..."
聽聽聽聽聽聽聽step
聽聽聽聽聽聽聽# loop_rest returns remaining trigger intervals.
聽聽聽聽聽聽聽unless $timer.loop_rest > 1
聽聽聽聽聽聽聽聽聽聽labeltext = "Click the Button"
聽聽聽聽聽聽聽聽聽聽reset
聽聽聽聽聽聽聽聽聽聽@btn.state = 'normal'
聽聽聽聽聽聽聽end
聽聽聽聽end

聽聽聽聽# Set initial window geometry; i.e., size and placement.
聽聽聽聽win_w, win_h = 300, 160
聽聽聽聽# root.minsize(win_w, win_h)
聽聽聽聽win_l = (TkWinfo.screenwidth('.') - win_w) / 2
聽聽聽聽root.geometry("#{win_w}x#{win_h}+#{win_l}+50")

聽聽聽聽# Set resize permissions.
聽聽聽聽root.resizable(false, false)

聽聽聽聽# Make Cmnd+Q work as expected.
聽聽聽聽root.bind('Command-q') {Tk.root.destroy}

聽聽聽聽Tk.mainloop
ensure
聽聽聽聽puts DEBUG unless DEBUG.empty?
end
</code>

Regards, Morton