• Publicidad

Quiero aprender a crear ventanas con Gtk y Perl

¿Ya sabes lo que es una referencia? Has progresado, el nível básico es cosa del pasado y ahora estás listo para el siguiente nivel.

Quiero aprender a crear ventanas con Gtk y Perl

Notapor proxy_lainux » 2009-04-30 22:37 @984

Hola.

Hace tiempo intenté aprender a utilizar Gtk, he hice un código... y ahorita vi que puedo hacerlo también con Perl... así que no sé si alguien puede ayudarme con una explicación... o algún tutorial para hacer ventanas con Gtk con Perl, ya que los manuales que encuentro me dan explicación sencillas y en este momento las estoy estudiando, pero como les comenté, hice un código para hacer eso. El problema es que hay cosas que no comprendo, por ejemplo... ¿cómo coloco dos botones?... Ya aprendí a colocar 1... ¿pero 2?...

Les dejo el código de Gtk para que vean que fue lo que hice, (el código está mal hecho ya que solo le agregué cosas para aprender) - y les dejo el código en perl que encontré.. pero no logro, por ejemplo, agregarle 2 botones.

Sintáxis: [ Descargar ] [ Ocultar ]
  1. include <gtk/gtk.h> 
  2.  
  3. void close(GtkWidget *widget, gpointer *data){ 
  4. gtk_main_quit(); 
  5. void click(GtkButton *button, gpointer *data){ 
  6. GtkLabel *label = GTK_LABEL(data); 
  7. gtk_label_set_text(label," hola mundo"); 
  8. gtk_misc_set_alignment(GTK_MISC(label),0,5); 
  9. GtkWidget *construct_window(void){ 
  10. GtkWidget *window, *vbox, *button, *label, *separator, *hbox; 
  11. window = gtk_window_new(GTK_WINDOW_TOPLEVEL); 
  12. gtk_window_set_title(GTK_WINDOW(window),"proxy _lainux"); 
  13. gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER_ALWAYS); 
  14. gtk_widget_set_usize(GTK_WIDGET(window),500,400); 
  15. gtk_widget_show(window); 
  16. hbox = gtk_hbox_new(TRUE,0); 
  17. gtk_container_add(GTK_CONTAINER(window),hbox); 
  18. gtk_widget_show(hbox); 
  19. vbox = gtk_vbox_new(TRUE,0); 
  20. gtk_container_add(GTK_CONTAINER(window),vbox); 
  21. gtk_widget_show(vbox); 
  22. label = gtk_label_new(" Anatamortem"); 
  23. gtk_misc_set_alignment(GTK_MISC(label),0,5); 
  24. gtk_box_pack_start(GTK_BOX(hbox),label,TRUE,TRUE,0); 
  25. gtk_container_set_border_width(GTK_CONTAINER(label),0); 
  26. gtk_widget_show(label); 
  27. separator = gtk_hseparator_new(); 
  28. gtk_box_pack_start(GTK_BOX(hbox),separator,TRUE,TRUE,0); 
  29. gtk_widget_show(separator); 
  30. button = gtk_button_new_with_label("Voltear"); 
  31. gtk_signal_connect(GTK_OBJECT(button),"clicked",GTK_SIGNAL_FUNC(click),label); 
  32. gtk_misc_set_alignment(GTK_MISC(button),0,5); 
  33. gtk_box_pack_start(GTK_BOX(vbox),button,TRUE,TRUE,0); 
  34. gtk_container_set_border_width(GTK_CONTAINER(button),10); 
  35. gtk_widget_show(button); 
  36. gtk_signal_connect(GTK_OBJECT(window),"destroy",GTK_SIGNAL_FUNC(close),NULL); 
  37. return window; 
  38.  
  39. int main(int argc, char *argv[]){ 
  40. gtk_init(&argc,&argv); 
  41. GtkWidget *window = construct_window(); 
  42. gtk_main(); 
  43. return 0; 

Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
#!/usr/bin/perl -w

use Gtk2 '-init';

$ventana = Gtk2::Window->new('toplevel');
$ventana->set_title('Hello World!');
$ventana->set_border_width(20);
$boton = Gtk2::Button->new('Hello World!');
$boton->signal_connect('clicked' => sub {Gtk2->main_quit; });
$ventana->add($boton);
$ventana->show_all;

# Empieza el show...
Gtk2->main;
Coloreado en 0.002 segundos, usando GeSHi 1.0.8.4
proxy_lainux
Perlero nuevo
Perlero nuevo
 
Mensajes: 30
Registrado: 2009-04-13 21:38 @943

Publicidad

Notapor explorer » 2009-05-01 05:05 @253

Si te fijas en cualquiera de los códigos, los botones se crean con new():
Sintáxis: [ Descargar ] [ Ocultar ]
Using c Syntax Highlighting
button = gtk_button_new_with_label("Voltear");
Coloreado en 0.001 segundos, usando GeSHi 1.0.8.4

Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
$boton = Gtk2::Button->new('Hello World!');
Coloreado en 0.001 segundos, usando GeSHi 1.0.8.4


Luego, esos botones son agregados a un contenedor:
Sintáxis: [ Descargar ] [ Ocultar ]
Using c Syntax Highlighting
gtk_container_set_border_width(GTK_CONTAINER(button),10);
Coloreado en 0.001 segundos, usando GeSHi 1.0.8.4

Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
$ventana->add($boton);
Coloreado en 0.001 segundos, usando GeSHi 1.0.8.4


El cómo agregar más botones consiste en repetir esa operación.

Por ejemplo, el siguiente trozo de código, extraído de este tutorial, de la página de gtk-perl:
Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
        # Create a new hbox with the appropriate homogeneous
        # and spacing settings
        $box = Gtk2::HBox->new($homogenous, $spacing);

        # Create a series of buttons with the appropriate settings
        $button = Gtk2::Button->new("Gtk2::HBox::pack");
        $box->pack_start($button, $expand, $fill, $padding);
        $button->show;

        $button = Gtk2::Button->new("(box,");
        $box->pack_start($button, $expand, $fill, $padding);
        $button->show;

        $button = Gtk2::Button->new("button,");
        $box->pack_start($button, $expand, $fill, $padding);
        $button->show;
Coloreado en 0.001 segundos, usando GeSHi 1.0.8.4

Como ves, primero se crea el contenedor, en este caso, un hbox (una caja con despliegue horizontal). Luego, crea 3 botones que va empaquetando en el $box uno a uno.
JF^D Perl programming & Raku programming. Grupo en Telegram: https://t.me/Perl_ES
Avatar de Usuario
explorer
Administrador
Administrador
 
Mensajes: 14477
Registrado: 2005-07-24 18:12 @800
Ubicación: Valladolid, España

Notapor proxy_lainux » 2009-05-01 14:27 @643

¡Ah!, ok, entonces es así como funciona HBox; me imagino que también funcionará con VBox... ¿no?

Bueno, gracias por el tutorial, voy a leerlo y espero entender más...

salu2 :D
proxy_lainux
Perlero nuevo
Perlero nuevo
 
Mensajes: 30
Registrado: 2009-04-13 21:38 @943


Volver a Intermedio

¿Quién está conectado?

Usuarios navegando por este Foro: No hay usuarios registrados visitando el Foro y 32 invitados

cron