Crear la función principal y los elementos de la interfaz de usuario
Ahora, creemos la función main
y configuremos la interfaz de usuario.
int main(int argc, char *argv[]) {
GtkWidget *window;
GtkWidget *grid;
GtkWidget *start_stop_button;
GtkWidget *reset_button;
gtk_init(&argc, &argv); // Inicializar GTK
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Stopwatch"); // Establecer el título de la ventana
gtk_window_set_default_size(GTK_WINDOW(window), 200, 200); // Establecer el tamaño predeterminado de la ventana
g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL); // Conectar el evento de cierre de la ventana
grid = gtk_grid_new(); // Crear un diseño de cuadrícula
gtk_container_add(GTK_CONTAINER(window), grid); // Agregar la cuadrícula a la ventana
label = gtk_label_new("00:00"); // Crear una etiqueta GTK que muestra inicialmente "00:00"
gtk_widget_set_hexpand(label, TRUE); // Permitir que la etiqueta se expanda horizontalmente
gtk_widget_set_vexpand(label, TRUE); // Permitir que la etiqueta se expanda verticalmente
gtk_grid_attach(GTK_GRID(grid), label, 0, 0, 1, 1); // Agregar la etiqueta a la cuadrícula
start_stop_button = gtk_button_new_with_label("Start"); // Crear un botón con la etiqueta "Start"
gtk_widget_set_hexpand(start_stop_button, TRUE); // Permitir que el botón se expanda horizontalmente
gtk_widget_set_vexpand(start_stop_button, TRUE); // Permitir que el botón se expanda verticalmente
g_signal_connect(G_OBJECT(start_stop_button), "clicked", G_CALLBACK(start_stop_button_clicked), NULL); // Conectar el evento de clic del botón
gtk_grid_attach(GTK_GRID(grid), start_stop_button, 0, 1, 1, 1); // Agregar el botón a la cuadrícula
reset_button = gtk_button_new_with_label("Reset"); // Crear un botón con la etiqueta "Reset"
gtk_widget_set_hexpand(reset_button, TRUE); // Permitir que el botón se expanda horizontalmente
gtk_widget_set_vexpand(reset_button, TRUE); // Permitir que el botón se expanda verticalmente
g_signal_connect(G_OBJECT(reset_button), "clicked", G_CALLBACK(reset_button_clicked), NULL); // Conectar el evento de clic del botón
gtk_grid_attach(GTK_GRID(grid), reset_button, 0, 2, 1, 1); // Agregar el botón a la cuadrícula
gtk_widget_show_all(window); // Mostrar la ventana y todos sus widgets secundarios
timer_id = g_timeout_add(1000, timer_callback, NULL); // Crear un temporizador que se activa cada 1000 ms (1 segundo) y llama a timer_callback
gtk_main(); // Iniciar el bucle principal de eventos de GTK
return 0;
}
- La función
main
configura la ventana GTK, las etiquetas, los botones y sus manejadores de eventos de clic.