CodeGym /Java Course /Python SELF EN /Working with Checkboxes, Radio Buttons, and Spinboxes

Working with Checkboxes, Radio Buttons, and Spinboxes

Python SELF EN
Level 50 , Lesson 2
Available

1. Checkboxes (Checkbutton)

A checkbox is a square button that can be either checked or unchecked. It's useful when you need to give users the option to select one or more parameters from a list.

Creating a Checkbox

To create a checkbox, use the Checkbutton widget. To track the state of the checkbox (checked or unchecked), a variable of type IntVar is typically used, which stores 1 if the checkbox is checked and 0 if it's unchecked.

Python
import tkinter as tk

# Function to check checkbox state
def show_status():
    print("On" if var.get() == 1 else "Off")

# Create window
root = tk.Tk()
root.title("Checkbox Example")

# Variable to hold state
var = tk.IntVar()

# Checkbox linked to var
checkbox = tk.Checkbutton(root, text="Enable Option", variable=var, command=show_status)
checkbox.pack(pady=20)

root.mainloop()

Code Explanation

  • variable=var: Links the checkbox to the var variable, which updates whenever the checkbox state changes.
  • Function show_status(): Outputs the current state of the checkbox to the console. If checked, it prints "On", otherwise "Off".

Using Multiple Checkboxes

If you need independent options, you can create several checkboxes, each linked to its own variable.

Python
# Variables to hold states
option1 = tk.IntVar()
option2 = tk.IntVar()

# Checkboxes
check1 = tk.Checkbutton(root, text="Option 1", variable=option1)
check2 = tk.Checkbutton(root, text="Option 2", variable=option2)
check1.pack()
check2.pack()

2. Radio Buttons (Radiobutton)

Radio buttons are a set of round buttons that let the user select only one option. They're useful when you need to provide users with a single choice out of multiple options.

Creating Radio Buttons

Use the Radiobutton widget to create radio buttons. To link the buttons and make their selection mutually exclusive, all buttons should be bound to a single variable (IntVar or StringVar).

Python
# Function to display selected option
def show_choice():
    print(f"Selected option: {choice.get()}")

# Variable to track selection
choice = tk.IntVar()

# Radio Buttons
radio1 = tk.Radiobutton(root, text="Option 1", variable=choice, value=1, command=show_choice)
radio2 = tk.Radiobutton(root, text="Option 2", variable=choice, value=2, command=show_choice)
radio3 = tk.Radiobutton(root, text="Option 3", variable=choice, value=3, command=show_choice)

radio1.pack()
radio2.pack()
radio3.pack()

Code Explanation

  • variable=choice: Links all radio buttons to the same choice variable to make them mutually exclusive.
  • value: Sets the value the choice variable will take when the user selects this radio button.
  • Function show_choice(): Displays the selected option in the console.

Radio Buttons with Text Values

You can also bind radio buttons to a StringVar variable to use text values.

Python
# Variable for text choice
color_choice = tk.StringVar()

# Radio buttons with text values
radio_red = tk.Radiobutton(root, text="Red", variable=color_choice, value="Red")
radio_blue = tk.Radiobutton(root, text="Blue", variable=color_choice, value="Blue")
radio_green = tk.Radiobutton(root, text="Green", variable=color_choice, value="Green")

radio_red.pack()
radio_blue.pack()
radio_green.pack()
... ...
1
Task
Python SELF EN, level 50, lesson 2
Locked
Creating and setting up checkboxes
Creating and setting up checkboxes
2
Task
Python SELF EN, level 50, lesson 2
Locked
Radio Buttons for Color Selection
Radio Buttons for Color Selection
3
Task
Python SELF EN, level 50, lesson 2
Locked
Application with Checkboxes, Radio Buttons, and a Slider
Application with Checkboxes, Radio Buttons, and a Slider
4
Task
Python SELF EN, level 50, lesson 2
Locked
Configurable Graphical Interface
Configurable Graphical Interface
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION