Skip to content
Ebru Akagündüz edited this page May 3, 2015 · 2 revisions

Progress bar shows percantage of the operation has been complemented. It is good to show progress of the issue to users. You can initalize it with Gtk::ProgressBar.new, demonstrated below example. The example also inherits Gtk::Window which is not necessary (only to show different way).

We have used super in following example so do not need to specify window variable, Gtk::Window already is inherited.

#!/usr/bin/ruby
require "gtk3"

class ProgressBarWindow < Gtk::Window

  def initialize
    super
    set_title("ProgressBar Example")
    set_border_width(10)

    vbox = Gtk::Box.new(:vertical, 6)
    add(vbox)

    # in a class, using variables with @ character, is instance variable
    # that means access it from everywhere in this class without 
    @progressbar = Gtk::ProgressBar.new
    vbox.pack_start(@progressbar, :expand => true, :fill => false, :padding =>0)

    button = Gtk::CheckButton.new("Show text")
    button.signal_connect("toggled") {|button| on_show_text_toggled(button)}
    vbox.pack_start(button, :expand => true, :fill => true, :padding => 0)

    button = Gtk::CheckButton.new("Activity mode")
    button.signal_connect("toggled") {|button| on_activity_mode_toggled(button)}
    vbox.pack_start(button, :expand => true, :fill => true, :padding => 0)

    button = Gtk::CheckButton.new("Right to Left")
    button.signal_connect("toggled") {|button| on_right_to_left_toggled(button)}
    vbox.pack_start(button, :expand => true, :fill => true, :padding => 0)

    @timeout_id = GLib::Timeout.add(50) {on_timeout}
    @activity_mode = false

    signal_connect("destroy"){Gtk.main_quit}
    show_all
  end

  def on_show_text_toggled(button)
    show_text = button.active?
    if show_text
      text = "some text"
    else
      text = nil
    end
    @progressbar.set_text(text)
    @progressbar.set_show_text(show_text)
  end

  def on_activity_mode_toggled(button)
    @activity_mode = button.active?
    if @activity_mode
      @progressbar.pulse
    else
     @progressbar.set_fraction(0.0)
    end
  end

  def on_right_to_left_toggled(button)
    value = button.active?
    @progressbar.set_inverted(value)
  end

  def on_timeout
    if @activity_mode
      @progressbar.pulse
    else
      new_value = @progressbar.fraction + 0.01
      if new_value > 1
        new_value = 0
      end
      @progressbar.set_fraction(new_value)
    end
    return true
  end
end
app = ProgressBarWindow.new
Gtk.main

Progress bar has two different mode; percentage and activiy mode. If the application knows amount of the work, use percentage mode; if not use activity mode to show the app is live.

For first choice use set_fraction(), for second use pulse() method.

Clone this wiki locally