0

The line: pics.box.signal_connect("button_press_event"){pics.nuImage}, triggers nuImage and adds 1 to the picindex counter upon clicking, making the current image destroy, and next image show. I would like to make this automatic, like a slideshow without having to click. It needs to show a new image every x amount of seconds, using a sleep or something like GLib.timeout_add_seconds (), but I do not understand how to implement these options to continue looping without any user input. Thank you for your help, I am very new to ruby.

require 'gtk2'

class Pics
  attr_accessor :pile, :picindex, :imgLoaded, :image, :box, :window, :time

def initialize
  @window = Gtk::Window.new()
  @window.signal_connect("destroy"){Gtk.main_quit}
  pic1 = "1.jpg"
  pic2 = "2.jpg"
  pic3 = "3.jpg"
  pic4 = "4.jpg"
  @pile = [pic1, pic2, pic3, pic4]
  @picindex = 0
  self.getImage
  @box = Gtk::EventBox.new.add(@image)
  @time = true
end

def nuImage
  @box.remove(@image)
  @picindex = @picindex + 1
  @picindex = 0 if @picindex == @pile.length
  self.getImage
  @box.add(@image)
  @box.show
end

def getImage
  @imgLoaded = @pile[@picindex]
  img = Gdk::Pixbuf.new(@imgLoaded, 556, 900)
  @image = Gtk::Image.new(img)
  @image.show
end

end # class Pics

pics = Pics.new
  pics.box.signal_connect("button_press_event"){pics.nuImage}
  pics.window.set_default_size(556, 900)
  pics.window.add(pics.box)
  pics.window.show_all

Gtk.main 
user3648577
  • 61
  • 1
  • 4

1 Answers1

0

the following code is an implementation:

GLib::Timeout.add(1000) do
  pics.nuImage if pics.time
  true
end

pics.window.signal_connect("key_press_event") do |_window, event|
  case event.keyval
  when Gdk::Keyval::GDK_KEY_space
    pics.time = !pics.time
  end
end

more details: http://ruby-gnome2.sourceforge.jp/hiki.cgi?GLib%3A%3ATimeout

related: Ruby GTK Pixbuf timed image change

Community
  • 1
  • 1
myokoym
  • 16
  • 2