GTK+ Find Selected Radio Button

Find selected radio button in a group of GTK+ 3 Glade radio buttons using the C programming language. The previous two blog posts showed how to group radio buttons in Glade and how to program radio buttons in C code. This article follows on showing how to read the states of the radio buttons and determine which radio button is selected.

Glade Radio Button Project

Before continuing, complete the previous two tutorials:

Make a copy of the radio button project called radio_3btn from the previous blog post (second link above). This project will be modified to read the state of each radio button.

Rename the project folder to radio_3btn_read. Also rename the project in the make file to the same name. The make file should look as follows.

makefile in the radio_3btn_read directory:

Modify the Glade File

Open the project’s Glade file. Select the top radio button and make the following changes at the bottom of the right pane in Glade. Click the Signals tab at the bottom right Glade pane. Expand GtkToggleButton and find the toggled signal. Change the toggled handler name to on_rb_toggled. Delete the user data. To do this, click the user data and in the dialog box that pops up, click the Clear button. Do the same for the other radio buttons. This will cause the same handler function to be called when any radio button is clicked. The image below shows the changes made to the first radio button rb_1.

Glade GTK Radio Button toggled Signal Handler
Glade GTK Radio Button toggled Signal Handler

The complete modified Glade file is included below. in the glade subdirectory:

GTK+ Find Selected Radio Button C Code

Below is the C code to find the selected radio button and update the text label according to the selected radio button. The code is based on the tutorial on how to get pointers to GTK Glade widgets. A structure is used to hold pointers to widgets in the main window. A pointer to the widget structure is passed to the radio button toggled handler function on_rb_toggled(). This is set up in main() as explained in the tutorial at the above link.

main.c found in the src subdirectory:

When any of the radio buttons is clicked, the on_rb_toggled() handler function is called. gtk_toggle_button_get_active() is used to check which radio button is currently selected. gtk_label_set_text() is used to update the text of the label for the selected radio button.

As with the previous tutorial, g_print() is used to print text to the command line. Start the program from a terminal window to see the text printed by g_print(). The results may surprise you again. The same text is printed twice for each click. This is because the radio button that is toggling from on to off emits a signal as well as the radio button that is toggling from off to on. The toggled signal handler is therefore called twice. Both times the currently selected radio button is detected using gtk_toggle_button_get_active(). Again, this may or may not be a problem, depending on the application.

Reading Radio Buttons in a Form

Most applications will not need to trigger a signal when a radio button is clicked, thus avoiding the double toggle problem. The application below modifies the above code to add a button and update the text label when the button is clicked. Make a copy of the above project and rename its folder to radio_3btn_read_btn. Also rename the project in the make file to the same name.


radio_3btn_read_btn project makefile in project’s directory:

Glade File

Modify the project’s Glade file to add a button. At the top right pane in Glade, click the GtkBox which should be called box1. At the bottom half of the right pane under the General tab, change the Number of items to 5 to add a slot for a button.

Click the Button icon in the left Glade pane under Control and Display. Now click in the new box slot to place the button on the main window. In the bottom half of the right Glade pane under the General tab, change the button ID to btn_read. Near the bottom of the same pane, change the button text to Read Radio Buttons under Label with optional image.

With the button still selected, add a signal handler function for the button under the Signals tab. Next to the clicked signal add a handler function called on_btn_read_clicked.

Go to each radio button and delete the toggled handler under the Signals tab. Radio button signals will not be used in this project. The completed Glade window should look as follows.

Find Selected Radio Button Glade Form
Find Selected Radio Button Glade Form

If there are any doubts as to the changes made, copy the complete Glade file below and open it in the Glade editor to see the changes. in the glade subdirectory:

C Source Code

In the C code, change the radio button toggled handler function for the button clicked handler. Now the label will be updated with the selected radio button text only when the button widget is clicked. The complete source code is shown below.

main.c in the src subdirectory:

Notice that when starting the application from the command line that the text printed by g_print() is only printed once for each button click.

Improving the Glade Layout

In the application window, the button widget stretches across the width of the window. Change the button to be positioned in the middle of the button by clicking the button to select it. Now click the Common tab in the bottom half of the right Glade pane. Scroll down to find the Widget Spacing heading and change Horizontal to Center.

In the same pane, space can be added above and below the button widget so that it is not so close to the text label above it and the bottom of the window. Still under Widget Spacing and the Margins subheading, change the Top and Bottom margins to 8 each. The changes are shown below

Changing Glade Widget Spacing
Changing Glade Widget Spacing

The final application window now looks as follows.

Find Selected Radio Button GTK Glade Application
Find Selected Radio Button GTK Glade Application

Development Tools

This application was developed using:

  • Linux Mint 18.1, Mate, 64-bit
  • Glade 3.18.3
  • GTK library (libgtk-3-dev) 3.18.9-1ubuntu3.3


4 thoughts on “GTK+ Find Selected Radio Button”

  1. I’m blazing through your tutorials today. I find learning much easier and more enjoyable when following along with someone else. Thank you for all the effort you have put into these tutorials!

Leave a Reply

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