miércoles, 20 de noviembre de 2013

Una interfaz sencilla utilizando GTK 3.0


Vamos a realizar un simple interfaz con cuatro botones a puro código utilizando GTK, que posteriormente nos servirá para controlar un pequeño mini-robot por medio de bluetooth. 


El código esta programado en C y utilizando las librerías de GTK 3.0 compilado y ejecutado en un entorno de Linux Mint por lo que comente el codigo  de lo que fui entendiendo: 

  //Autor : José Luis Cobian
// @cobymotion
// Work with GTK 
//noviembre 2013

#include 
//method for window destroy 
static gboolean delete_window(GtkWidget *widget, GdkEvent *event, gpointer data){
     gtk_main_quit();
     return FALSE;
}

int main (int argc, char *argv[]) {

    //A program that has a window and four buttons to control the mini-robot
    //Declaration of variables or components 
    GtkWidget *window; 
    GtkWidget *buttonUp;
    GtkWidget *buttonDown;
    GtkWidget *buttonLeft;
    GtkWidget *buttonRight;
    GtkWidget *grid;
    //initialization 
    gtk_init (&argc, &argv);
    /// create a new Window 
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    // Set title for window
    gtk_window_set_title (GTK_WINDOW (window), "Control automatico");
    // Set window resize 
    gtk_window_set_default_size(GTK_WINDOW(window), 385, 395);
    //create grid
    grid = gtk_grid_new();
    //Expand the window across the grid
    gtk_grid_set_row_homogeneous(GTK_GRID(grid),TRUE);
    gtk_grid_set_column_homogeneous(GTK_GRID(grid),TRUE);
    //add container 
    gtk_container_add(GTK_CONTAINER(window),grid);
    // Method for destroying the window    
    g_signal_connect(window,"delete_event",G_CALLBACK(delete_window),NULL);
    //Create buttons and append in to Grid
    buttonUp = gtk_button_new_with_label("Avanzar");
    gtk_grid_attach(GTK_GRID(grid),buttonUp,1,0,1,1);
    buttonDown = gtk_button_new_with_label("Atras");
    gtk_grid_attach(GTK_GRID(grid),buttonDown,1,4,1,1);
    buttonLeft = gtk_button_new_with_label("Izquierda");
    gtk_grid_attach(GTK_GRID(grid),buttonLeft,0,2,1,1);
    buttonRight = gtk_button_new_with_label("Derecha");
    gtk_grid_attach(GTK_GRID(grid),buttonRight,4,2,1,1);    
    //show the widgets
    
     gtk_widget_show_all (window);
     gtk_main ();

}


Para compilarlo utilizamos: 

gcc carro.c -o carro `pkg-config --cflags --libs gtk+-3.0`


Espero les sirva 




No hay comentarios:

Publicar un comentario