RubyLit - Ruby.del.litoral!
Empaquetando con VBox y HBox

Es una algo habitual en un programa Ruby GTK agrupar (empaquetar) widgets con cajas horizontales y/o verticales.

la api especifica que para crear una caja se dispone del metodo new con los siguientes parametros


Gtk::HBox.new(homogeneous = false, spacing = nil)

  • El primer parametro acepta valores true o false, y se utiliza para determinar si todos los widgets que agreran a la caja tendran el mismo tamaño.
  • El segundo parametro es para especificar el espaciado entre los widgets.

El siguiente programa que ilustra distinas posibilidades utilizando vbox, hbox y labels.

A los lectores particularmente curiosos les puede resultar interesante la opción del menu “Cambiar Posición” ya que no hay mucha documentación en la web de como acceder a las “Child Properties”


require 'gtk2'

class Mi_app < Gtk::VBox
    def initialize()
        super()
        # Create the menubar
        menubar = Gtk::MenuBar.new
        pack_start( menubar, false, false, 0)

        # Create our toplevel menuitem
        top_menu = Gtk::MenuItem.new( "Sistema" )
        menubar.append( top_menu )

        menu = Gtk::Menu.new
        item = Gtk::MenuItem.new( "Cambiar posición" )
        menu.append( item )
        item.signal_connect( "activate" ) { |widget, event, y| 
            mi_metodo
        }

        item = Gtk::ImageMenuItem.new(Gtk::Stock::ABOUT)
        menu.append( item )
        item.signal_connect( "activate" ) { |widget, event, y| 
#            ilogo = Gdk::Pixbuf.new("logo-joshua.png",40,40)

            acerca_de = Gtk::AboutDialog.new()
            acerca_de.name = "App Base" 
            acerca_de.website = "http://www.rubylit.com.ar" 
            acerca_de.copyright="2007, Rafael Bidegain" 
            acerca_de.authors = ["Rafael Bidegain"]
            acerca_de.license = "GPL V3 o superior" 
            acerca_de.version = "1.0" 
#            acerca_de.logo = ilogo
            acerca_de.run
        }

        separador = Gtk::SeparatorMenuItem.new()
        menu.append( separador )

        item = Gtk::ImageMenuItem.new(Gtk::Stock::QUIT)
        menu.append( item )
        item.signal_connect( "activate" ) { |widget, event, y| 
            Gtk.main_quit 
        }
        top_menu.set_submenu( menu )

        # --------------------------------------------        
        # poner a partir de acá los widgets de la app
        # --------------------------------------------
        vbox = Gtk::VBox.new(true, nil)

        @hbox1  = Gtk::HBox.new(false, 0)
        hbox2  = Gtk::HBox.new(false, 0)
        hbox3  = Gtk::HBox.new(false, 0)
        hbox4  = Gtk::HBox.new(false, 0)

        sep1    = Gtk::HSeparator.new()
        sep2    = Gtk::HSeparator.new()
        sep3    = Gtk::HSeparator.new()

        @l1a = Gtk::Label.new("|-|")
        l2a = Gtk::Label.new("|--|")
        l3a = Gtk::Label.new("|---|")
        l4a = Gtk::Label.new("|----|")
        l5a = Gtk::Label.new("|-----|")
        @hbox1.pack_start(@l1a, true, true)
        @hbox1.pack_start(l2a, true, true)
        @hbox1.pack_start(l3a, true, true)
        @hbox1.pack_start(l4a, true, true)
        @hbox1.pack_start(l5a, true, true)

        l1b = Gtk::Label.new("|-|")
        l2b = Gtk::Label.new("|--|")
        l3b = Gtk::Label.new("|---|")
        l4b = Gtk::Label.new("|----|")
        l5b = Gtk::Label.new("|-----|")
        hbox2.pack_start(l1b, true, false)
        hbox2.pack_start(l2b, false, false)
        hbox2.pack_start(l3b, false, false)
        hbox2.pack_start(l4b, false, false)
        hbox2.pack_start(l5b, false, false)

        l1c = Gtk::Label.new("|-|")
        l2c = Gtk::Label.new("|--|")
        l3c = Gtk::Label.new("|---|")
        l4c = Gtk::Label.new("|----|")
        l5c = Gtk::Label.new("|-----|")
        hbox3.pack_start(l1c, false, false)
        hbox3.pack_start(l2c, true, false)
        hbox3.pack_start(l3c, false, false)
        hbox3.pack_start(l4c, false, false)
        hbox3.pack_start(l5c, false, false)

        l1d = Gtk::Label.new("|-|")
        l2d = Gtk::Label.new("|--|")
        l3d = Gtk::Label.new("|---|")
        l4d = Gtk::Label.new("|----|")
        l5d = Gtk::Label.new("|-----|")
        hbox4.pack_start(l1d, true, false)
        hbox4.pack_start(l2d, true, false)
        hbox4.pack_start(l3d, false, false)
        hbox4.pack_start(l4d, false, false)
        hbox4.pack_start(l5d, false, false)

        vbox.pack_start(@hbox1, false, false)
        vbox.pack_start(sep1, true, true)
        vbox.pack_start(hbox2, false, false)
        vbox.pack_start(sep2, true, true)
        vbox.pack_start(hbox3, false, false)
        vbox.pack_start(sep3, true, true)
        vbox.pack_start(hbox4, false, false)

        pack_start( vbox, false, false, 0)
    end
    def mi_metodo()
        if @hbox1.child_get_property(@l1a,'position') == 0
            @hbox1.reorder_child(@l1a,2)
        else
            @hbox1.reorder_child(@l1a,0)
        end
    end
end

class Viewer < Gtk::Window
  def initialize()
    super()
    set_title("App Base") 
    set_window_position(Gtk::Window::POS_CENTER)
    signal_connect("delete_event") { |i,a| Gtk::main_quit }
    set_default_size(250, 250)
    add(Mi_app.new)
  end
end

Gtk.init()

view = Viewer.new
view.show_all

Gtk.main()


hbox y vbox