MaestroGarcia
MaestroGarcia

Reputation: 13

How to use progressbar indeterminate in ruby/tk?

I cant figure out how the progressbar in ruby/tk works in indeterminate mode.

I can get the progressbar showing but it will not move.

an example if I just use this line in my code

progressBar.start

If I only have that line the progressbar will be showing and moving as it should.

But if i add line of code under it it will not execute. I had the impression that the .start method of progressbar starts it and the method stop stops it and in between you have the code that should be executed and the progressbar show until it sees the stop.

But if i do this

progressBar.start 
# some code (a loop that takes a long while to execute)
progressBar.stop

the progressbar is not beeing shown until the code in between is finished? I thought that was why you wanted the progressbar?

What am I not understanding here.


Thx for you help but why is not this working. Its just a stupid test. But i am doing somthing similar in the real program. If I write this code the progressbar will not run as it should but instead after the Devil img loop is done?

Dir.chdir("c:/temp")

  bilder = Dir.glob("*.jpg")+
  bilder = Dir.glob("*.png")+
  bilder = Dir.glob("*.gif")
  puts bilder

 root = TkRoot.new { title 'Progressbar Demo' }
 content = Tk::Frame.new(root) {}.grid(:sticky =>'new')
 progress =  Tk::Tile::Progressbar.new(content:mode=>'indeterminate':orient=>:horizontal)
 progress.pack

Thread.new do
progress.start
i=0                                                
    while i < bilder.length                              
    Devil.with_image(bilder[i]) do |img|               
    img.thumbnail2(150)
    img.save("thumbnail_"+ bilder[i])    
    end                
  i=i+1                                                  
 end
 progress.stop
 end
 Tk.mainloop

Upvotes: 1

Views: 1059

Answers (1)

peter
peter

Reputation: 42192

It is a threading problem. This code should work. Tested on Win7, Ruby 1.9.3

require 'tk'

root = TkRoot.new { title 'Progressbar Demo' }
progress = Tk::Tile::Progressbar.new(root, :mode=>'determinate', :orient=>:horizontal, :maximum=>100)
progress.pack

Thread.new do
 99.times do |i|
   progress.step(1) #or progress.value = i
   puts i
   sleep 0.1
 end
end

Tk.mainloop

For your indeerminate version that is

require 'tk'

root = TkRoot.new { title 'Progressbar Demo' }
progress = Tk::Tile::Progressbar.new(root, :mode=>'indeterminate', :orient=>:horizontal)
progress.pack

Thread.new do
  progress.start
  99.times do |i|
   puts i
   sleep 0.1
  end
  progress.stop
end

Tk.mainloop

Here an example how to do it wiht green shoes

require 'green_shoes'

Shoes.app do
  @p = progress
  @animate = animate do |percent|
    @animate.stop if percent > 99
    puts percent
    @p.fraction = percent.to_f / 100
  end
end

EDIT: based on the added question here a reworked more rubylike version of your script unfortionatly i had to comment out the Devil lines cause i can't get this gem to work (loaderror)

require 'tk'
# require 'devil'

bilder = Dir['c:/temp/*.{jpg,png,gif}']
root = TkRoot.new { title 'Progressbar Demo' }
progress =  Tk::Tile::Progressbar.new(root, :mode=>'indeterminate', :orient=>'horizontal')
progress.pack
STDOUT.sync = true

Thread.new do
  progress.start
  bilder.each do |bild|
    puts bild
    sleep 0.5
    # Devil.with_image(bild) do |img|
    #   img.thumbnail2(150)
    #   img.save("thumbnail_#{bild}")    
    # end                
  end
  progress.stop
end
Tk.mainloop

LAST EDIT:

here a working example with mini_magick since devil doesn't work on any of my pc's and gives problem with TK

['mini_magick','tk','fileutils'].each(&method(:require))

bilder = Dir['c:/test/*.{jpg,png,gif}']
root = TkRoot.new { title 'Progressbar Demo' }
progress =  Tk::Tile::Progressbar.new(root, :mode=>'indeterminate', :orient=>'horizontal')
progress.pack
STDOUT.sync = true

def generate file, out, type
  image = MiniMagick::Image.open file
  if type == :thumb
    image.resize "92x92"
  elsif type == :slide
    image.resize "800x600"
  end
  image.write out
end

Thread.new do
  progress.start
  bilder.each do |bild|
    puts bild
    generate bild, bild+'.thumb.jpg', :thumb
  end
  progress.stop
  progress.destroy
end
Tk.mainloop

Upvotes: 3

Related Questions