[Ruby/Tk example] Feedback [incr Widget]

Puts up a window containing a progress bar and a button. Clicking on the button, hides the button and starts showing progress in the progress bar. When the progress bar has filled, the button returns 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: August 31, 2006
#
# Progress Indicator

require 'tk'
require 'tkextlib/iwidgets'

DEBUG = []

begin
聽聽聽聽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'=>4)
聽聽聽聽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)
聽聽聽聽def btn.action
聽聽聽聽聽聽聽unpack
聽聽聽聽聽聽聽default = @fb.labeltext
聽聽聽聽聽聽聽@fb.labeltext = "Doing Something ..."
聽聽聽聽聽聽聽(1..@fb.steps).each do |i|
聽聽聽聽聽聽聽聽聽聽@fb.step
聽聽聽聽聽聽聽聽聽聽sleep(0.2)
聽聽聽聽聽聽聽end
聽聽聽聽聽聽聽@fb.labeltext = default
聽聽聽聽聽聽聽@fb.reset
聽聽聽聽聽聽聽pack('pady'=>10)
聽聽聽聽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(true, true)
聽聽聽聽root.resizable(false, false)

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

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

Regards, Morton