Use GTK3 progress bars in Ruby - ruby

This is my code:
require "gtk3"
builder_file = "#{File.expand_path(File.dirname(__FILE__))}/example.ui"
builder = Gtk::Builder.new(:file => builder_file)
window = builder.get_object("window")
window.signal_connect("destroy") { Gtk.main_quit }
progressbar=builder.get_object("progressbar1")
progressbar.set_fraction(0.5)
button = builder.get_object("button1")
button.signal_connect("clicked") {
for i in(0..4)
sleep(1)
puts "query " + (i+1).to_s + " done"
a=(i+1)/5.to_f
puts a
progressbar.set_fraction(a)
end
}
Gtk.main
sleep is just a placeholder for a web query that takes about 1 second. When I execute this code on my machine, the console output is fine, but the progress bar stays empty until it jumps to full after five seconds, which is not what I want. How can I make use of the progress bar?

The integration of the event loop of the GUI tool (Gtk, Tk, etc) and Ruby annoys beginners including me. I know two ways.
1.add thread
require 'gtk3'
win = Gtk::Window.new('Sample')
win.signal_connect('destroy') { Gtk.main_quit }
box = Gtk::Box.new(:vertical)
pb = Gtk::ProgressBar.new
pb.set_fraction(0.5)
b = Gtk::Button.new(label: 'Button')
b.signal_connect('clicked') do
Thread.new do
5.times do |i|
sleep 1
pb.fraction = (i + 1) / 5.0
end
end
end
win.add box
box.pack_start pb
box.pack_start b
win.show_all
Gtk.main
2.use GLib::Timeout
b.signal_connect('clicked') do
i = 0.0
GLib::Timeout.add(1000) do |a|
i += 0.2
pb.fraction = i
i < 1.0 # return false and stop when i >= 1.0
end
end

Related

Two version of the same code not giving the same result

I am trying to implement a simple timeout class that handles timeouts of different requests.
Here is the first version:
class MyTimer
def handleTimeout mHash, k
while mHash[k] > 0 do
mHash[k] -=1
sleep 1
puts "#{k} : #{mHash[k]}"
end
end
end
MAX = 3
timeout = Hash.new
timeout[1] = 41
timeout[2] = 5
timeout[3] = 14
t1 = MyTimer.new
t2 = MyTimer.new
t3 = MyTimer.new
first = Thread.new do
t1.handleTimeout(timeout,1)
end
second = Thread.new do
t2.handleTimeout(timeout,2)
end
third = Thread.new do
t3.handleTimeout(timeout,3)
end
first.join
second.join
third.join
This seems to work fine. All the timeouts work independently of each other.
Screenshot attached
The second version of the code however produces different results:
class MyTimer
def handleTimeout mHash, k
while mHash[k] > 0 do
mHash[k] -=1
sleep 1
puts "#{k} : #{mHash[k]}"
end
end
end
MAX = 3
timeout = Hash.new
timers = Array.new(MAX+1)
threads = Array.new(MAX+1)
for i in 0..MAX do
timeout[i] = rand(40)
# To see timeout value
puts "#{i} : #{timeout[i]}"
end
sleep 1
for i in 0..MAX do
timers[i] = MyTimer.new
threads[i] = Thread.new do
timers[i].handleTimeout( timeout, i)
end
end
for i in 0..MAX do
threads[i].join
end
Screenshot attached
Why is this happening?
How can I implement this functionality using arrays?
Is there a better way to implement the same functionality?
In the loop in which you are creating threads by using Thread.new, the variable i is shared between main thread (where threads are getting created) and in the threads created. So, the value of i seen by handleTimeout is not consistent and you get different results.
You can validate this by adding a debug statement in your method:
#...
def handleTimeout mHash, k
puts "Handle timeout called for #{mHash} and #{k}"
#...
end
#...
To fix the issue, you need to use code like below. Here parameters are passed to Thread.new and subsequently accessed using block variables.
for i in 0..MAX do
timers[i] = MyTimer.new
threads[i] = Thread.new(timeout, i) do |a, b|
timers[i].handleTimeout(a, b)
end
end
More on this issue is described in When do you need to pass arguments to Thread.new? and this article.

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
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

ruby Tk app running in while loop displays twice and then stops responding

I'm pretty new to ruby and am trying to implement a Tk application that will display a window prompting for input at a certain interval. In between the interval I want the window to not display in any taskbars, etc. and so I've implemented the following code that seems to work perfectly the first time through, but after the window displays the second time and I enter text in the TkEntry and click the TkButton the window is dismissed and never returns. I've tried putting in some "puts" calls at key locations to see what is happening and it seems that it never even makes it past the call to "displayUi".
*EDIT:
I'm running ruby 1.9.3p385 (2013-02-06) [i386-mingw32] on a Windows 7 system (in case that makes any difference)
Any help (even if it's providing a different mechanism to accomplish the same goal) would be appreciated, but please keep in mind that I'm a ruby noobie. Thanks!
require "tk"
class Sample
attr_accessor :root, :active
#active = false
def initialize
# init
end
def entry (task)
# do some work here
#active = false
end
def displayUi ()
#active = true
if (#root.nil?)
#root = TkRoot.new { title "Sample App" }
else
# already running just restart
Tk.restart
end
TkLabel.new(#root) {
text 'Sample Text'
pack { padx 15; pady 15; side 'left' }
}
statusInput = TkEntry.new(#root) {
pack('side'=>'left', 'padx'=>10, 'pady'=>10)
}
statusInput.focus
response = TkVariable.new
statusInput.textvariable = response
TkButton.new(#root, :text => "Ok", :command => proc { entry(response.value); #root.destroy }) {
pack('side'=>'left', 'padx'=>10, 'pady'=>10)
}
Tk.mainloop
end
end
i=0
st = Sample.new
while (true)
if (!st.active)
st.displayUi()
end
sleep(1)
end

Ruby Pause thread

In ruby, is it possible to cause a thread to pause from a different concurrently running thread.
Below is the code that I've written so far. I want the user to be able to type 'pause thread' and the sample500 thread to pause.
#!/usr/bin/env ruby
# Creates a new thread executes the block every intervalSec for durationSec.
def DoEvery(thread, intervalSec, durationSec)
thread = Thread.new do
start = Time.now
timeTakenToComplete = 0
loopCounter = 0
while(timeTakenToComplete < durationSec && loopCounter += 1)
yield
finish = Time.now
timeTakenToComplete = finish - start
sleep(intervalSec*loopCounter - timeTakenToComplete)
end
end
end
# User input loop.
exit = nil
while(!exit)
userInput = gets
case userInput
when "start thread\n"
sample500 = Thread
beginTime = Time.now
DoEvery(sample500, 0.5, 30) {File.open('abc', 'a') {|file| file.write("a\n")}}
when "pause thread\n"
sample500.stop
when "resume thread"
sample500.run
when "exit\n"
exit = TRUE
end
end
Passing Thread object as argument to DoEvery function makes no sense because you immediately overwrite it with Thread.new, check out this modified version:
def DoEvery(intervalSec, durationSec)
thread = Thread.new do
start = Time.now
Thread.current["stop"] = false
timeTakenToComplete = 0
loopCounter = 0
while(timeTakenToComplete < durationSec && loopCounter += 1)
if Thread.current["stop"]
Thread.current["stop"] = false
puts "paused"
Thread.stop
end
yield
finish = Time.now
timeTakenToComplete = finish - start
sleep(intervalSec*loopCounter - timeTakenToComplete)
end
end
thread
end
# User input loop.
exit = nil
while(!exit)
userInput = gets
case userInput
when "start thread\n"
sample500 = DoEvery(0.5, 30) {File.open('abc', 'a') {|file| file.write("a\n")} }
when "pause thread\n"
sample500["stop"] = true
when "resume thread\n"
sample500.run
when "exit\n"
exit = TRUE
end
end
Here DoEvery returns new thread object. Also note that Thread.stop called inside running thread, you can't directly stop one thread from another because it is not safe.
You may be able to better able to accomplish what you are attempting using Ruby Fiber object, and likely achieve better efficiency on the running system.
Fibers are primitives for implementing light weight cooperative
concurrency in Ruby. Basically they are a means of creating code
blocks that can be paused and resumed, much like threads. The main
difference is that they are never preempted and that the scheduling
must be done by the programmer and not the VM.
Keeping in mind the current implementation of MRI Ruby does not offer any concurrent running threads and the best you are able to accomplish is a green threaded program, the following is a nice example:
require "fiber"
f1 = Fiber.new { |f2| f2.resume Fiber.current; while true; puts "A"; f2.transfer; end }
f2 = Fiber.new { |f1| f1.transfer; while true; puts "B"; f1.transfer; end }
f1.resume f2 # =>
# A
# B
# A
# B
# .
# .
# .

Getting 'Gtk:ERROR' trying to run WxRuby

I'm using Ubuntu 10.04 and I'm trying to run a WxRuby example which I copy-pasted from this site WxRubyWiki. I've been looking for help over the net but I couldn't find anything similar...
I'm getting this error...
Gtk:ERROR:/build/buildd/gtk+2.0-2.20.1/gtk/gtkwindow.c:6789:IA__gtk_window_present_with_time: assertion failed: (widget->window != NULL)
Aborted
These are the versions I'm using...
ruby -v
ruby 1.8.7 (2010-01-10 patchlevel 249) [i486-linux]
gem list
...
wxruby (2.0.1 x86-linux)
...
And this is the code I'm trying to run...
require 'rubygems' if RUBY_VERSION < '1.9'
require 'wx'
class EventFrame < Wx::Frame
def initialize()
super(nil, -1, "Event Frame")
#idleCounter = 0
evt_close {|event| on_close(event)}
evt_idle {|event| on_idle(event)}
evt_size {|event| on_size(event)}
evt_key_down {|event| on_key(event)}
evt_left_down {|event| on_left_down(event)}
# You can still process these events, you just need to define a separate callback for middle_down and right_down
# to process them as separate events
evt_middle_down {|event| on_middle_down(event)}
evt_right_down {|event| on_right_down(event)}
button = Wx::Button.new(self, -1, "Push me")
evt_button(button.get_id()) {|event| on_button(event)}
show()
end
def message(text, title)
m = Wx::MessageDialog.new(self, text, title, Wx::OK | Wx::ICON_INFORMATION)
m.show_modal()
end
def on_close(event)
message("This frame will be closed after you push ok", "Close event")
#close(true) - Don't call this - it will call on_close again, and your application will be caught in an infinite loop
# Either call event.skip() to allow the Frame to close, or call destroy(), as follows
destroy()
end
def on_idle(event)
#idleCounter += 1
if #idleCounter > 15 # Without the counter to slow this down, Idle events would be firing every second
message("The system is idle right now", "Idle event")
#idleCounter = 0
end
event.request_more() # You must include this, otherwise the Idle event won't occur again
end
def on_size(event)
size = event.get_size()
x = size.x
y = size.y
message("X = " + x.to_s + ", Y = " + y.to_s, "Size event")
end
def on_key(event)
message("Key pressed", "Key Event")
end
def on_left_down(event)
button = ""
if event.left_down()
button = "Left"
end
message(button + " button was clicked", "Mouse event")
end
def on_middle_down(event)
# This method hasn't been implemented yet...
#if event.middle_down()
#button = "Middle"
#end
message("Middle button was clicked", "Mouse event")
end
def on_right_down(event)
# This method hasn't been implemented yet...
#if event.right_down()
#button = "Right"
#end
message("Right button was clicked", "Mouse event")
end
def on_button(event)
message("Button was clicked", "Button event")
end
end
class MyApp < Wx::App
def on_init
EventFrame.new()
end
end
MyApp.new.main_loop
Thanks in advance!
It is more of an understanding of how GTK+2/wx/wxRuby works. As is, the code above does not work on your configuration in a Virtual Box machine I set to test this nor my development machine's Ubuntu 11.10 x86_64 with 1000Hz kernel compile option and ruby 1.9.3 p21.
The GTK+2 error occurs when the on_size event fires during frame creation. The creation is not yet complete so the message box does not have a parent at that time. You can test this by commenting out (in def on_size(event):
message("X = " + x.to_s + ", Y = " + y.to_s, "Size event")
and trying:
puts "Size event: X = #{x}, Y = #{y}"
to see the event details on standard out. You will notice there are two events fired during creation; an initial size event and a resize event
Another caveat is the idle loop which locks Unity o my system. You can test that the idle event is happening without a lock up by changing the code as below:
in def initialize add this before show():
create_status_bar(2)
self.status_text = "Welcome to wxRuby!"
then in idle_event:
def on_idle(event)
#idleCounter += 1
#if #idleCounter > 15 # Without the counter to slow this down, Idle events would be firing every second
# message("The system is idle right now", "Idle event")
# #idleCounter = 0
#end
set_status_text #idleCounter.to_s, 1
event.request_more() # You must include this, otherwise the Idle event won't occur again
end
A last caveat about the code is you may notice you don't see the key or mouse down events creating their message box. That is because the button control fills the client area and traps the frame events for keys and buttons. If you resize the frame while the app is running the button will not resize with it by default (GTK+2 platform). If you then click inside the frame client area but not on the button you will see mouse events.
Good Luck !
this fixed and works in ubuntu 11.10 :)

Resources