Skip to content

Instantly share code, notes, and snippets.

@huycozy
Last active July 1, 2024 05:05
Show Gist options
  • Save huycozy/e256eb638287f3ce5799c53f881bcce9 to your computer and use it in GitHub Desktop.
Save huycozy/e256eb638287f3ce5799c53f881bcce9 to your computer and use it in GitHub Desktop.
Simple GTK text example
  1. Create gtk_text_example.c file:
#include <gtk/gtk.h>

// Callback function to handle the "destroy" signal
void on_destroy(GtkWidget *widget, gpointer data) {
    gtk_main_quit();
}

int main(int argc, char *argv[]) {
    // Initialize GTK
    gtk_init(&argc, &argv);

    // Create the main window
    GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window), "GTK Text Example");
    gtk_container_set_border_width(GTK_CONTAINER(window), 10);
    gtk_widget_set_size_request(window, 300, 200);

    // Create a label with some text
    GtkWidget *label = gtk_label_new("Hello, GTK!");

    // Add the label to the window
    gtk_container_add(GTK_CONTAINER(window), label);

    // Connect the "destroy" signal to the callback function
    g_signal_connect(window, "destroy", G_CALLBACK(on_destroy), NULL);

    // Show all the widgets
    gtk_widget_show_all(window);

    // Start the GTK main loop
    gtk_main();

    return 0;
}
  1. Compile it
gcc -o gtk_text_example gtk_text_example.c $(pkg-config --cflags --libs gtk+-3.0)
./gtk_text_example
  1. Run the executable output file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment