信号定时ruby gtk图像更改没有button_press_event,如何触发循环

时间:2014-07-20 02:07:58

标签: ruby timer gtk sleep glib

行:pics.box.signal_connect(“button_press_event”){pics.nuImage},触发nuImage并在点击时将1添加到picindex计数器,使当前图像消失,并显示下一个图像。我想自动制作,就像幻灯片一样,无需点击。它需要每隔x秒显示一个新图像,使用睡眠或类似GLib.timeout_add_seconds(),但我不明白如何实现这些选项以继续循环而无需任何用户输入。谢谢你的帮助,我对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 

1 个答案:

答案 0 :(得分:0)

以下代码是一个实现:

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

更多详情:http://ruby-gnome2.sourceforge.jp/hiki.cgi?GLib%3A%3ATimeout

相关:Ruby GTK Pixbuf timed image change