DEV Community

Divyanshu Shekhar
Divyanshu Shekhar

Posted on

GTK Button & Signal – GUI Programming In C

A Button is a standard GTK Widget, and supports many features like:

1.You can change the label of the Button
2.Change the appearance of the Button
3.Provide a keyboard shortcut for the Button
4.Also, Disable the Button and Activate when a condition is fulfilled

Creating GTK Button

In the previous blog about GTK, we just created a new window. Now we will create a labeled button and display it on the same window.

#include <gtk/gtk.h>

int main(int argc,char *argv[])
{
    gtk_init(&argc,&argv);
    GtkWidget *win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    GtkWidget *btn = gtk_button_new_with_label("Click Here");
    gtk_container_add(GTK_CONTAINER(win),btn);
    gtk_widget_show_all(win);
    gtk_main();
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

GtkWidget *btn = gtk_button_new_with_label("Click Here"); – In the GTK library, there is a standard function to create buttons i.e gtk_button_new function, but the newer version of GTK provides us with gtk_button_new_with_label allows us to add a text label to the button at the same time we create the button.

gtk_container_add(GTK_CONTAINER(win),btn); – The gtk_container_add function places the button inside the window, that we have created.

gtk_widget_show_all(win); – Previous blog, we used gtk_widget_show and this time it is replaced by gtk_widget_show_all. This function tells GTK to show all the named widgets and also the widget that the named widget contains.

Learn More about GTK Button and Signal from the original post.

Discussion (0)