Food order GUI displaying Tkinter radiobutton and checkbutton widgets.
GUI showing Checkbutton and Radiobutton widgets in Tkinter.

Today’s tutorial is going to focus on two commonly used Tkinter widgets – Checkbutton and Radiobutton. Buttons can be seen everywhere in GUI applications. They are useful for allowing users to interact with the window, creating options and choices for a user to select. In a previous tutorial we took a look at using the Tkinter Button widget.

Creating Checkbutton Widgets

Checkbuttons are a very commonly used widget. They are great whenever a user needs to select multiple items in the GUI window, or when you need to control a feature of an application, switching it on and off with the checkbutton. This functionality is useful when you want to enable/disable features such as showing or hiding a toolbar.

Checkbutton widgets can also be used to call Python methods or custom-made functions using the command argument. Every checkbutton needs to have a corresponding variable associated with it. Control variables created using the variable classes – IntVar, DoubleVar, BooleanVar, and StringVar –can be used to keep track of changes to Tkinter widgets.

Let’s take a look at a small snippet of code that sets up a Checkbutton. If you are new to Tkinter, you can check out a tutorial on how to get started creating GUIs.

First, we create the integer variable, var, which is used to monitor the state of the checkbutton. Next, we create an instance of Checkbutton. For parameters, we set its root parent widget, the text, the variable associated with the widget, and the function it will call when the user interacts with it. Lastly, we use pack() to arrange the widget in the window.

In the selectItems() method, we use get() to read the variable’s current value. If you ever want to specify the value of a variable, then use the set() method.

Creating Radiobutton Widgets

Whereas the Checkbutton widget is useful for selecting a number of choices, the Radiobutton is generally used for picking one choice out of many. Radiobuttons can also be used to call Python functions when clicked on.

In order to achieve exclusivity among the radiobuttons in a group, they should share the same variable. This means that when one radiobutton in a group is selected any other selected widgets will be deselected. You can see this demonstrated in the bit of code below.

You can see how the radiobutton widgets look in the following image.

Example of Tkinter radiobutton widgets.
Tkinter Radiobutton example.

The process for creating radiobuttons is similar to checkbuttons. One additional parameter we need to pass is the value associated with each individual radiobutton. If you have a bunch of radiobutton widgets you need to create, try using a for loop to instantiate them.

A Simple GUI Example

Now let’s take a look at an example that combines the Checkbutton and Radiobutton widgets into a very simple food ordering GUI.

NOTE: We use Object-Oriented Programming (OOP) in the following program. If you are not familiar with OOP, please check out another tutorial to help get you started. [Coming next week: April 6, 2020]

The complete GUI can be seen in the image at the beginning of this tutorial.

Explanation

Start by importing the modules we need from tkinter. We need to create a class for our window, SimpleGUI, that inherits from Tk. Next, initialize the interface’s parameters.

In the setupWindow() function, we set up the Label, Checkbutton and Radiobutton widgets, and their respective variables. A for loop is used to create each checkbutton instance. Refer to the code for comments about how to set up the checkbutton widgets. The next widgets we need to create are the radiobuttons. By sharing the var object, we create a mutually exclusive group of radiobuttons. Mutual exclusivity refers to the theory in probability that two or more events cannot occur at the same time. 

Finally, when the next_button is clicked, the printResults() function is called. This prints the text from selected checkbuttons and radiobutton and closes the program.

Tkinter radiobutton and checkbutton output.
Output to the Terminal window.

Summary

In this tutorial we take a brief look at the Checkbutton and Radiobutton widgets. These two kinds of widgets are useful for allowing a user to select choices from a group of options. Checkbuttons are typically used when a user needs to be able to select multiple selections; Radiobuttons are best used when a user only needs to select one choice from many.

For more information about Tkinter or the Checkbutton and Radiobutton widgets, check out the Python documentation on Tkinter.

Twitter
Visit Us
Follow Me
LinkedIn
Share

Leave a Reply

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