GtkToggleButton Glade GTK C Program Tutorial

This GtkToggleButton Glade GTK C program tutorial shows how to use a toggle button and read its state using C code. The user interface is laid out using the Glade user interface designer. GTK+ 3 and the C programming language is used in the project example.

Part 18 of GTK 3 Programming with C and Glade Tutorial

See the full GTK3 tutorial index

A toggle button differs from a plain button in that when it is clicked it remembers its state. When the button is clicked for the first time, it changes from an inactive state to and active state. When the button is clicked again, it changes from active, back to inactive. In other words, it toggles state whenever it is clicked.

The image below shows the simple application built in this tutorial. On the left of the image is the application window with toggle button inactive and active. On the right is the toggle button widget as it appears in Glade.

GtkToggleButton Glade GTK C Program
GtkToggleButton Glade GTK C Program

How the GtkToggleButton Application is Constructed

This tutorial is based on the following previous tutorials.

See the GTK Glade C Programming index for a series of tutorials that show how to get started with Glade GTK+ 3 C programming.

In the tutorial steps that follow, the toggle button application is created from the template files. Glade is used to lay out the GUI. C code is then written to create the functionality of the application. In the complete application, if the user clicks the toggle button, the text label at the top of the application displays the state of the button as active. When the user clicks the toggle button again, the state of the button is shown as inactive.

GtkToggleButton Glade GTK C Program Tutorial Steps

Before starting, create a set of template files to use as a starting point. This will give you a make file, a C source file and a Glade file.

1. Name the Project

Open the make file and name the project toggle_button at the top of the file as shown below.

# change application name here (executable output name)

The complete make file looks as follows.


# change application name here (executable output name)
# compiler
# debug
# optimisation
# warnings
GTKLIB=`pkg-config --cflags --libs gtk+-3.0`
# linker
LDFLAGS=$(PTHREAD) $(GTKLIB) -export-dynamic
OBJS=    main.o
all: $(OBJS)
	$(LD) -o $(TARGET) $(OBJS) $(LDFLAGS)
main.o: src/main.c
	$(CC) -c $(CCFLAGS) src/main.c $(GTKLIB) -o main.o
	rm -f *.o $(TARGET)

2. Build the User Interface in Glade

Open the file found in the glade subdirectory of the project with Glade user interface designer for editing. The steps that follow show how to place the three widgets used in the project in Glade. They can be seen in the image below as they appear in the right pane of Glade.

Widgets Place in Glade for the Toggle Button Application
Widgets Place in Glade for the Toggle Button Application

2.1 Place a Box

Place a box container of type GtkBox in the main window (box1 in the above image. When placing the box, change the number of items from to 2 (rather than the default 3). Leave the default orientation of the box widget, which is Vertical.

Spacing for the box widget can be changed to 4 in the Spacing field under the General tab in Glade. Under the Common tab, all four margins can be changed to 3 (top, bottom, left and right margins under the Margins heading). Adding this spacing gives the widgets a better appearance in the application window.

2.2 Place a Label

Place a label widget in the top open slot of the box widget that was placed in the previous step. Change the ID of the box in Glade to lbl_button_state under the General tab. Change the Label field of the label to Button State: Inactive also under the General tab in Glade.

2.3 Place a Toggle Button

Place a toggle button in the open slot of the box widget. The toggle button is found under Control and Display in the left pane in Glade and is shown in the image at the top of this tutorial.

Change the ID of the button to togglebuttonState and the label of the button to Click to Change Button State under the General tab in Glade.

Under the Signals tab in Glade, add a handler name of on_togglebuttonState_toggled next to toggled. Click next to toggled under User data and select lbl_button_state in the dialog box that pops up. This passes a pointer to the label widget to the toggle button callback function in the C code. This is explained in the tutorial on getting a pointer to a widget in a callback function.

After making the above changes, they should appear as follows in Glade.

Adding a Toggle Button Handler in Glade
Adding a Toggle Button Handler in Glade

The complete listing of the Glade file from the glade subdirectory is shown below.

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.18.3 -->
  <requires lib="gtk+" version="3.12"/>
  <object class="GtkWindow" id="window_main">
    <property name="can_focus">False</property>
    <property name="title" translatable="yes">Toggle Button</property>
    <signal name="destroy" handler="on_window_main_destroy" swapped="no"/>
      <object class="GtkBox" id="box1">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="margin_left">3</property>
        <property name="margin_right">3</property>
        <property name="margin_top">3</property>
        <property name="margin_bottom">3</property>
        <property name="orientation">vertical</property>
        <property name="spacing">4</property>
          <object class="GtkLabel" id="lbl_button_state">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="label" translatable="yes">Button State: Inactive</property>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          <object class="GtkToggleButton" id="togglebuttonState">
            <property name="label" translatable="yes">Click to Change Button State</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
            <signal name="toggled" handler="on_togglebuttonState_toggled" object="lbl_button_state" swapped="no"/>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">1</property>

3. Write the Application Code in C

Edit main.c found in the src subdirectory as follows.


#include <gtk/gtk.h>
int main(int argc, char *argv[])
    GtkBuilder      *builder; 
    GtkWidget       *window;
    gtk_init(&argc, &argv);
    builder = gtk_builder_new();
    gtk_builder_add_from_file (builder, "glade/", NULL);
    window = GTK_WIDGET(gtk_builder_get_object(builder, "window_main"));
    gtk_builder_connect_signals(builder, NULL);
    return 0;

// lbl_button_state is set in User data field of Signals tab in Glade - this maps to text_label
void on_togglebuttonState_toggled(GtkToggleButton *togglebutton, GtkLabel *text_label)
	gboolean button_state;
	button_state = gtk_toggle_button_get_active(togglebutton);
	if (button_state) {
		// button is active
		gtk_label_set_text(text_label, "Button State: Active");
	else {
		// button is inactive
		gtk_label_set_text(text_label, "Button State: Inactive");
// called when window is closed
void on_window_main_destroy()

4. Build and Run the Toggle Button Application

After following the above tutorial steps to create the application, open the project directory in a command line terminal window and enter make to build the project. If the build was a success, the application can be run by entering ./toggle_button at the command prompt.

If the project was constructed correctly, clicking the toggle button in the application will show the state of this button in the text label as shown in the image at the top of this page.

How the GtkToggleButton Application Works

When the toggle button is clicked, on_togglebuttonState_toggled() is called. This is the callback or handler function for the button that was edited in Glade. Because the user data that is passed to this function was set to be a pointer to the label in glade, the text of this label can be changed in this function.

In the toggle button handler, gtk_toggle_button_get_active() is used to read the state of the toggle button and change the text of the top label accordingly.

See the documentation for the GtkToggleButton widget for more information on this widget.

3 thoughts on “GtkToggleButton Glade GTK C Program Tutorial”

  1. I have worked through all of your C GTK tutorials and learned quite a bit. I look forward to future tutorial postings. Thanks again for making this site!

Leave a Reply

Your email address will not be published. Required fields are marked *