Los elementos en una aplicación que utiliza Ruby Gnome 2 puede hacer uso de pestañas para la organización de sus elementos y esto se puede lograr de una forma muy fácil como se muestra a continuación en el ejemplo:
#!/usr/bin/env ruby
require 'gtk2'
ventana=Gtk::Window.new
ventana.set_title("Titulo Ventana")
ventana.border_width=10
ventana.set_default_size(500,500)
ventana.set_window_position(Gtk::Window::POS_MOUSE)
#CREANDO EL CONTENEDOR PESTAÑA
pestana=Gtk::Notebook.new
#CREANDO LA ETIQUETA PARA LA PRIMERA PESTAÑA
etiPestana1=Gtk::Label.new("Pestaña 1")
#AHORA AGREGUEMOS ALGUNAS PESTAÑAS CADA UNA
#DE ELLAS CON UTILIZANDO UN CONTENEDOR EXPLICADO
#CON ANTERIORIDAD PARA HACERLO UN POCO MAS INTERESANTE
#AGREDANDO UNA TABLA COMO PESTAÑA
tabla=Gtk::Table.new(2,2,true)
#CREACION DE ELEMENTOS DE LA TABLA
etiqueta1=Gtk::Label.new("Etiqueta 1")
etiqueta2=Gtk::Label.new("Etiquera 2")
boton1=Gtk::Button.new("Boton 1")
boton2=Gtk::Button.new("Boton 2")
#OPCIONES PARA QUE LOS ELEMENTOS SE LLENEN TODA LA VENTANA
opciones = Gtk::EXPAND|Gtk::FILL
#ESQUINA SUPERIOR IZQUIERDA CUADRICULA 2X2
tabla.attach(etiqueta1,0,1,0,1,opciones,opciones,0,0)
#ESQUINA SUPERIOR DERECHA CUADRICULA 2X2
tabla.attach(etiqueta2,0,1,1,2,opciones,opciones,0,0)
#ESQUINA INFERIOR IZQUIERDA CUADRICULA 2X2
tabla.attach(boton1,1,2,0,1,opciones,opciones,0,0)
#ESQUINA INFERIOR DERECHA CUADRICULA 2X2
tabla.attach(boton2,1,2,1,2,opciones,opciones,0,0)
#AGREGANDO ETIQUETA DE PESTAÑA Y ELEMENTOS
pestana=pestana.append_page(tabla,etiPestana1)
#AGREGANDO EL CONTENEDOR DE PESTAÑA A LA VENTANA
ventana.add(pestana)
ventana.show_all
Gtk.main
El resultado del código anterior da como resultado lo siguiente:
jueves, 30 de diciembre de 2010
Ruby Gnome 2 - Parte 1 - Contenedores Fijos
Otra diferente forma de organizar los elementos en pantalla utilizando Ruby Gnome 2 son los contenedores fijos, estos permiten el posicionamiento de los elementos en una posicion "x,y" especifica, como se muestra en el siguiente ejemplo:
#!/usr/bin/env ruby
require 'gtk2'
ventana=Gtk::Window.new
ventana.border_width=10
ventana.set_title("Titulo Ventana")
ventana.set_default_size(300,300)
ventana.set_window_position(Gtk::Window::POS_MOUSE)
#CREACION DE VENTANA CON ELEMENTOS FIJOS
contenedorFijo=Gtk::Fixed.new
#CREACION DE ELEMENTOS PARA LA VENTANA
boton1=Gtk::Button.new("Boton 1")
boton2=Gtk::Button.new("Boton 2")
etiqueta1=Gtk::Label.new("Etiqueta 1")
#AGREGANDO ELEMENTOS AL CONTENEDOR
#parametros=nombreElemento,posicionX,posicionY
contenedorFijo.put(boton1,0,50)
contenedorFijo.put(boton2,100,223)
contenedorFijo.put(etiqueta1,125,75)
ventana.add(contenedorFijo)
ventana.show_all
Gtk.main
El resultado del codigo anterior es:
#!/usr/bin/env ruby
require 'gtk2'
ventana=Gtk::Window.new
ventana.border_width=10
ventana.set_title("Titulo Ventana")
ventana.set_default_size(300,300)
ventana.set_window_position(Gtk::Window::POS_MOUSE)
#CREACION DE VENTANA CON ELEMENTOS FIJOS
contenedorFijo=Gtk::Fixed.new
#CREACION DE ELEMENTOS PARA LA VENTANA
boton1=Gtk::Button.new("Boton 1")
boton2=Gtk::Button.new("Boton 2")
etiqueta1=Gtk::Label.new("Etiqueta 1")
#AGREGANDO ELEMENTOS AL CONTENEDOR
#parametros=nombreElemento,posicionX,posicionY
contenedorFijo.put(boton1,0,50)
contenedorFijo.put(boton2,100,223)
contenedorFijo.put(etiqueta1,125,75)
ventana.add(contenedorFijo)
ventana.show_all
Gtk.main
El resultado del codigo anterior es:
Ruby Gnome 2 - Parte 1 - Contenedores Tablas
Otro elemento que se puede utilizar ademas de los contenedores verticales y horizontales en las ventanas es el contenedor de tabla, el cual es capaz de organizar los elementos en una cuadricula, como se muestra en el siguiente ejemplo:
#!/usr/bin/env ruby
require 'gtk2'
#CREACION DE LA VENTANA PRINCIPAL
ventana=Gtk::Window.new
ventana.set_title("Titulo Ventana")
ventana.border_width=10
ventana.set_default_size(300,300)
ventana.set_window_position(Gtk::Window::POS_MOUSE)
#CREACION DE LA TABLA DE 2X2 PARA ORDENAR LOS ELEMENTOS
#LOS PARAMETROS SON EL ANCHO,ALTO Y SI SON HOMOGENEOS
tabla=Gtk::Table.new(2,2,true)
#CREACION DE ELEMENTOS DE LA TABLA
etiqueta1=Gtk::Label.new("Etiqueta 1")
etiqueta2=Gtk::Label.new("Etiquera 2")
boton1=Gtk::Button.new("Boton 1")
boton2=Gtk::Button.new("Boton 2")
#LOS ELEMENTOS SE AGREGAN A LA TABLA ESTABLECIENDO
#EL PUNTO DE LA ESQUINA SUPERIOR IZQUIERDA
#Y EL PUNTO DE LA ESQUINA INFERIOR DERECHA
#OPCIONES PARA QUE LOS ELEMENTOS SE LLENEN TODA LA VENTANA
opciones = Gtk::EXPAND|Gtk::FILL
#AGREGANDO BOTONES Y ETIQUETAS
# parametros=nombreElemento,x1,x2,y1,y2,opciones,opciones,xpad,ypad
#ESQUINA SUPERIOR IZQUIERDA CUADRICULA 2X2
tabla.attach(etiqueta1,0,1,0,1,opciones,opciones,0,0)
#ESQUINA SUPERIOR DERECHA CUADRICULA 2X2
tabla.attach(etiqueta2,0,1,1,2,opciones,opciones,0,0)
#ESQUINA INFERIOR IZQUIERDA CUADRICULA 2X2
tabla.attach(boton1,1,2,0,1,opciones,opciones,0,0)
#ESQUINA INFERIOR DERECHA CUADRICULA 2X2
tabla.attach(boton2,1,2,1,2,opciones,opciones,0,0)
#AGREGANDO TABLA EN LA VENTANA PRINCIPAL
ventana.add(tabla)
#MOSTRANDO ELEMENTOS
ventana.show_all
Gtk.main
El resultado del codigo anterior será el siguiente:
#!/usr/bin/env ruby
require 'gtk2'
#CREACION DE LA VENTANA PRINCIPAL
ventana=Gtk::Window.new
ventana.set_title("Titulo Ventana")
ventana.border_width=10
ventana.set_default_size(300,300)
ventana.set_window_position(Gtk::Window::POS_MOUSE)
#CREACION DE LA TABLA DE 2X2 PARA ORDENAR LOS ELEMENTOS
#LOS PARAMETROS SON EL ANCHO,ALTO Y SI SON HOMOGENEOS
tabla=Gtk::Table.new(2,2,true)
#CREACION DE ELEMENTOS DE LA TABLA
etiqueta1=Gtk::Label.new("Etiqueta 1")
etiqueta2=Gtk::Label.new("Etiquera 2")
boton1=Gtk::Button.new("Boton 1")
boton2=Gtk::Button.new("Boton 2")
#LOS ELEMENTOS SE AGREGAN A LA TABLA ESTABLECIENDO
#EL PUNTO DE LA ESQUINA SUPERIOR IZQUIERDA
#Y EL PUNTO DE LA ESQUINA INFERIOR DERECHA
#OPCIONES PARA QUE LOS ELEMENTOS SE LLENEN TODA LA VENTANA
opciones = Gtk::EXPAND|Gtk::FILL
#AGREGANDO BOTONES Y ETIQUETAS
# parametros=nombreElemento,x1,x2,y1,y2,opciones,opciones,xpad,ypad
#ESQUINA SUPERIOR IZQUIERDA CUADRICULA 2X2
tabla.attach(etiqueta1,0,1,0,1,opciones,opciones,0,0)
#ESQUINA SUPERIOR DERECHA CUADRICULA 2X2
tabla.attach(etiqueta2,0,1,1,2,opciones,opciones,0,0)
#ESQUINA INFERIOR IZQUIERDA CUADRICULA 2X2
tabla.attach(boton1,1,2,0,1,opciones,opciones,0,0)
#ESQUINA INFERIOR DERECHA CUADRICULA 2X2
tabla.attach(boton2,1,2,1,2,opciones,opciones,0,0)
#AGREGANDO TABLA EN LA VENTANA PRINCIPAL
ventana.add(tabla)
#MOSTRANDO ELEMENTOS
ventana.show_all
Gtk.main
El resultado del codigo anterior será el siguiente:
martes, 7 de diciembre de 2010
Ruby Gnome 2 - Parte 1 - Contenedores Horizontales y Verticales
Para poder organizar elementos en las ventanas ruby-gnome2 hace uso de los que se conoce como "contenedores". Existen varios tipos de contenedores pero por el momento solo explicare como utilizar los contenedores horizontales y verticales. También utilizare el ejemplo para mostrar como se crean botones y etiquetas.
Contenedores Horizontales
Como su nombre lo indica estos sirven para poder ordenar los elementos de una forma horizontal como se muestra en el siguiente ejemplo:
#!/usr/bin/env ruby
require 'gtk2'
ventana=Gtk::Window.new
ventana.set_title("Titulo Ventana")
ventana.set_default_size(300,300)
ventana.set_window_position(Gtk::Window::POS_MOUSE)
#Creacion de elementos para la ventana
#Creacion de boton 1
boton1=Gtk::Button.new("Boton 1")
#Creacion de boton 2
boton2=Gtk::Button.new("Boton 2")
#Creacion de etiqueta 1
etiqueta1=Gtk::Label.new("Etiqueta 1")
#Creacion de etiqueta 2
etiqueta2=Gtk::Label.new("Etiqueta 2")
#Creacion del contenedor horizontal
contenedorH=Gtk::HBox.new
#Funcion para agregar un elemento al contenedor
contenedorH.pack_start(boton1)
contenedorH.pack_start(etiqueta1)
contenedorH.pack_start(boton2)
contenedorH.pack_start(etiqueta2)
#AHORA QUE SE TIENE EL CONTENEDOR HORIZONTAL CON LOS ELEMENTOS
#ES NECESARIO AGREGAR EL CONTENEDOR A LA VENTANA
ventana.add(contenedorH)
#Se muestra la ventana
ventana.show_all
Gtk.main
El resultado del código anterior sera el siguiente:
Contenedores Verticales.
Ahora un ejemplo para la creación de contenedores verticales
#!/usr/bin/env ruby
require 'gtk2'
ventana=Gtk::Window.new
ventana.set_title("Titulo Ventana")
ventana.set_default_size(300,300)
ventana.set_window_position(Gtk::Window::POS_MOUSE)
#Creacion de elementos para la ventana
#Creacion de boton 1
boton1=Gtk::Button.new("Boton 1")
#Creacion de boton 2
boton2=Gtk::Button.new("Boton 2")
#Creacion de etiqueta 1
etiqueta1=Gtk::Label.new("Etiqueta 1")
#Creacion de etiqueta 2
etiqueta2=Gtk::Label.new("Etiqueta 2")
#Creacion del contenedor vertical
contenedorH=Gtk::VBox.new
#Funcion para agregar un elemento al contenedor
contenedorH.pack_start(boton1)
contenedorH.pack_start(etiqueta1)
contenedorH.pack_start(boton2)
contenedorH.pack_start(etiqueta2)
#AHORA QUE SE TIENE EL CONTENEDOR HORIZONTAL CON LOS ELEMENTOS
#ES NECESARIO AGREGAR EL CONTENEDOR A LA VENTANA
ventana.add(contenedorH)
#Se muestra la ventana
ventana.show_all
Gtk.main
El resultado del codigo anterior es:
#!/usr/bin/env ruby
require 'gtk2'
ventana=Gtk::Window.new
ventana.set_title("Titulo Ventana")
ventana.set_default_size(300,300)
ventana.set_window_position(Gtk::Window::POS_MOUSE)
#Creacion de elementos para la ventana
#Creacion de boton 1
boton1=Gtk::Button.new("Boton 1")
#Creacion de boton 2
boton2=Gtk::Button.new("Boton 2")
#Creacion de etiqueta 1
etiqueta1=Gtk::Label.new("Etiqueta 1")
#Creacion de etiqueta 2
etiqueta2=Gtk::Label.new("Etiqueta 2")
#Creacion del contenedor vertical
contenedorH=Gtk::VBox.new
#Funcion para agregar un elemento al contenedor
contenedorH.pack_start(boton1)
contenedorH.pack_start(etiqueta1)
contenedorH.pack_start(boton2)
contenedorH.pack_start(etiqueta2)
#AHORA QUE SE TIENE EL CONTENEDOR HORIZONTAL CON LOS ELEMENTOS
#ES NECESARIO AGREGAR EL CONTENEDOR A LA VENTANA
ventana.add(contenedorH)
#Se muestra la ventana
ventana.show_all
Gtk.main
El resultado del codigo anterior es:
Ruby Gnome 2 - Parte 1 - Creación de Ventanas
Bueno para comenzar a aprender como utilizar Ruby-Gnome2 lo primero que se necesita conocer es como poder crear una ventana y los metodos basicos de las ventanas:
Ejemplo 1:
#!/usr/bin/env ruby
#Libreria Ruby-Gnome2
require 'gtk2'
#Creacion de la ventana principal
ventana=Gtk::Window.new
#Establecer titulo de la ventana
ventana.set_title("Titulo Ventana")
#Establecer el tamaño de la ventana
ventana.set_default_size(300,300)
#Establecer la posicion de la ventana
#posibles opciones
#Gtk::Window::POS_NONE
#Gtk::Window::POS_CENTER
#Gtk::Window::POS_MOUSE
#Gtk::Window::POS_CENTER_ALWAYS
#Gtk::Window::POS_CENTER_ON_PARENT
#Establecer la posicion de la ventana en el mismo lugar
#donde se encuentra el mouse
ventana.set_window_position(Gtk::Window::POS_MOUSE)
#Se muestra la ventana
ventana.show_all
Gtk.main
El resultado del codigo anterior sera es siguiente:
Ejemplo 1:
#!/usr/bin/env ruby
#Libreria Ruby-Gnome2
require 'gtk2'
#Creacion de la ventana principal
ventana=Gtk::Window.new
#Establecer titulo de la ventana
ventana.set_title("Titulo Ventana")
#Establecer el tamaño de la ventana
ventana.set_default_size(300,300)
#Establecer la posicion de la ventana
#posibles opciones
#Gtk::Window::POS_NONE
#Gtk::Window::POS_CENTER
#Gtk::Window::POS_MOUSE
#Gtk::Window::POS_CENTER_ALWAYS
#Gtk::Window::POS_CENTER_ON_PARENT
#Establecer la posicion de la ventana en el mismo lugar
#donde se encuentra el mouse
ventana.set_window_position(Gtk::Window::POS_MOUSE)
#Se muestra la ventana
ventana.show_all
Gtk.main
El resultado del codigo anterior sera es siguiente:
Suscribirse a:
Entradas (Atom)





