# Problems with Ruby/Tk TkProgressbar

**URL:** <https://www.ruby-forum.com/t/problems-with-ruby-tk-tkprogressbar/154782>\
**Category:** Ruby\
**Created:** [December 3, 2008, 9:49pm UTC](https://www.ruby-forum.com/t/problems-with-ruby-tk-tkprogressbar/154782 "2008-12-03T21:49:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [December 3, 2008, 9:49pm UTC](https://www.ruby-forum.com/t/problems-with-ruby-tk-tkprogressbar/154782/1 "2008-12-03T21:49:56Z")

</div>

This doesn’t seem to exist, even though irb lists it in tab-completion  
mode. Anyone know how to get it working?

Thanks,  
Ken

---

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [December 3, 2008, 10:35pm UTC](https://www.ruby-forum.com/t/problems-with-ruby-tk-tkprogressbar/154782/2 "2008-12-03T22:35:53Z")

</div>

From: Kenneth McDonald [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: Problems with Ruby/Tk TkProgressbar  
Date: Thu, 4 Dec 2008 05:43:51 +0900  
Message-ID: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> This doesn’t seem to exist, even though irb lists it in tab-completion  
> mode. Anyone know how to get it working?

TkProgressbar is a widget which is one of Ttk (Tile) widget set.  
It requires Ttk (Tile) extension or Tcl/Tk8.5.x.

# Tcl/Tk8.5 has Ttk widget set as default.

## If you don’t have Ttk, the following example may be help you. It is also an example of TkComposite module.

require ‘tk’

class MyProgressBar \< TkWindow  
include TkComposite

def initialize\_composite(keys = {})  
# defaults  
bar\_color = ‘gray60’  
trough\_color = ‘gray90’  
text\_color = ‘black’  
reverse\_color = ‘white’

```
font_size = -12

@width = 150
@height = 20

# create widgets
@font = TkFont.new(['Helvetica', font_size])

@var = TkVariable.new('')

@frame.configure(:borderwidth=>3, :relief=>:ridge)

@size_base = TkFrame.new(@frame, :width=>@width, :height=>@height,
                         :borderwidth=>0).pack(:expand=>false,
                                               :fill=>:none)

@size_base.pack_propagate(false)

@base_label = TkLabel.new(@size_base, :textvariable=>@var, 

```

:font=\>@font,  
:highlightthickness=\>0, :borderwidth=\>0,  
:foreground=\>text\_color,  
:background=\>trough\_color,  
:padx=\>1, :pady=\>1).pack(:fill=\>:both,  
:expand=\>true)  
@prog\_bar = TkFrame.new(@base\_label,  
:highlightthickness=\>0, :borderwidth=\>0  
).place(:x=\>0, :y=\>0,  
:anchor=\>:nw,  
:relwidth=\>0.0, :relheight=\>1.0)

```
@prog_label = TkLabel.new(@prog_bar, :textvariable=>@var, 

```

:font=\>@font,  
:foreground=\>reverse\_color,  
:background=\>bar\_color,  
:highlightthickness=\>0, :borderwidth=\>0,  
:padx=\>1, :pady=\>1).place(:x=\>0, :y=\>0,  
:width=\>@width,  
:height=\>@height,  
:anchor=\>:nw)

```
# for bindings
@size_base.bindtags <<= @frame
@base_label.bindtags <<= @frame
@prog_bar.bindtags <<= @frame
@prog_label.bindtags <<= @frame

# delegates
delegate('DEFAULT', @frame)
delegate('background', @prog_label)
delegate('foreground', @base_label)

delegate_alias('barcolor', 'background', @prog_label)
delegate_alias('troughcolor', 'background', @base_label)
delegate_alias('textcolor', 'foreground', @base_label)
delegate_alias('reversecolor', 'foreground', @prog_label)

delegate_alias('fontfamily', 'family', @font)
delegate_alias('fontsize', 'size', @font)
delegate_alias('fontweight', 'weight', @font)
delegate_alias('fontslant', 'slant', @font)

option_methods(:width, :height, :font)

configure(keys) unless keys.empty?

```

end

def set(rate, text=nil)  
@prog\_bar.place(:relwidth=\>rate.to\_f)  
@var.value = text if text  
end

def get  
[TkPlace.current\_configinfo(@prog\_bar, :relwidth)[‘relwidth’],  
@var.value]  
end

def width(size=nil)  
if size  
@size\_base.width = size  
@prog\_label.place(:width=\>size)  
@width = size  
else  
@width  
end  
end

def height(size=nil)  
if size  
@size\_base.height = size  
@prog\_label.place(:height=\>size)  
@height = size  
else  
@height  
end  
end

def font(fnt=nil)  
if fnt  
if fnt.kind\_of?(Hash)  
@font.configure(fnt)  
else  
fnt = TkComm.simplelist(fnt) if fnt.kind\_of?(String)  
conf = {}  
conf[‘family’] = fnt.shift if fnt[0]  
conf[‘size’] = fnt.shift if fnt[0]  
fnt.each{|param|  
case param.to\_s  
when ‘normal’, ‘bold’  
conf[‘weight’] = param  
when ‘roman’, ‘italic’  
conf[‘slant’] = param  
when ‘underline’  
conf[‘underine’] = true  
when ‘overstrike’  
conf[‘overstrike’] = true  
end  
}

```
    @font.configure(conf)
  end
else
  @font
end

```

end  
end

if **FILE** == $0  
bar1 = MyProgressBar.new.pack  
bar2 = MyProgressBar.new(:width=\>120, :height=\>30,  
:barcolor=\>‘blue’, :troughcolor=\>‘white’,  
:fontsize=\>14, :fontweight=\>:bold).pack  
bar3 = MyProgressBar.new(:font=\>‘courier 8 bold’,  
:textcolor=\>‘red’, :reversecolor=\>‘yellow’,  
:width=\>250, :height=\>12).pack

bar1.set(0.0, ‘0%’)  
bar2.set(0.0, ‘0%’)  
bar3.set(0.0, ‘0%’)

bar1.bind(‘1’){p bar1.get}

timer = TkTimer.new(50, 100){|timerobj|  
cnt = timerobj.return\_value + 1  
bar1.set(cnt/100.0, “#{cnt}%”)  
bar2.set(cnt/100.0, “#{cnt}%”)  
bar3.set(cnt/100.0, “#{cnt}%”)  
cnt  
}

b = TkButton.new(:text=\>‘restart’, :command=\>proc{  
bar1.set(0.0, ‘0%’)  
bar2.set(0.0, ‘0%’)  
bar3.set(0.0, ‘0%’)  
timer.restart{0}  
}).pack  
b.invoke

Tk.mainloop  
end
