Introduction

Basic Widgets

Toplevel Widgets

Geometry Management

Binding Functions

Working with Images in Tkinter

Tkinter Advance

Applications and Projects

RadioButton in Tkinter

RadioButtons in Tkinter are useful for allowing users to select one option from a predefined set of options. Here's a simple tutorial on how to use Radiobutton widgets in Tkinter:

1. Initial Setup:

Start by importing the required modules and setting up the main window:

import tkinter as tk

root = tk.Tk()
root.title("RadioButton Tutorial")
root.geometry("300x200")

2. Create a Control Variable:

For Radiobutton, we often use a control variable (either IntVar or StringVar), which will hold the current selection.

selected_option = tk.IntVar()  # This will store the value associated with the selected Radiobutton

3. Define a Callback Function (Optional):

A callback function is used to perform some action when a RadioButton is selected:

def display_selection():
    selected_label.config(text=f"Selected Option: {selected_option.get()}")

4. Create Radiobutton Widgets:

We'll create three RadioButtons for demonstration:

radio1 = tk.Radiobutton(root, text="Option 1", value=1, variable=selected_option, command=display_selection)
radio1.pack(pady=5)

radio2 = tk.Radiobutton(root, text="Option 2", value=2, variable=selected_option, command=display_selection)
radio2.pack(pady=5)

radio3 = tk.Radiobutton(root, text="Option 3", value=3, variable=selected_option, command=display_selection)
radio3.pack(pady=5)

Here:

  • text is the label next to the RadioButton.
  • value is what the IntVar (selected_option in this case) will be set to when this RadioButton is selected.
  • variable is the control variable (IntVar) which will hold the value of the selected RadioButton.
  • command is an optional callback function that's triggered when the RadioButton is selected.

5. Display the Selected Option:

selected_label = tk.Label(root, text="Selected Option: None")
selected_label.pack(pady=20)

6. Main Loop:

Finally, run the Tkinter main loop:

root.mainloop()

When you run the complete script, you'll get a simple GUI with three RadioButtons. Clicking on any RadioButton will update the label to display the selected option. The control variable (selected_option) will hold the value (1, 2, or 3) associated with the selected RadioButton, allowing you to easily determine or use the user's choice elsewhere in your application.

1. How to use RadioButtons in Tkinter:

RadioButtons in Tkinter are used to create a group of options where only one option can be selected at a time. They are typically used to represent mutually exclusive choices.

import tkinter as tk

root = tk.Tk()
root.title("RadioButtons Example")

# Create a variable to store the selected option
selected_option = tk.StringVar()

# Create RadioButtons
radio_button1 = tk.Radiobutton(root, text="Option 1", variable=selected_option, value="Option 1")
radio_button2 = tk.Radiobutton(root, text="Option 2", variable=selected_option, value="Option 2")
radio_button3 = tk.Radiobutton(root, text="Option 3", variable=selected_option, value="Option 3")

# Pack RadioButtons
radio_button1.pack()
radio_button2.pack()
radio_button3.pack()

root.mainloop()

2. Creating a group of RadioButtons in Tkinter:

To create a group of RadioButtons, use the same variable (variable) for each button in the group. This ensures that only one option can be selected at a time.

# ... (previous code)

# Create a group of RadioButtons
radio_button4 = tk.Radiobutton(root, text="Option 4", variable=selected_option, value="Option 4")
radio_button5 = tk.Radiobutton(root, text="Option 5", variable=selected_option, value="Option 5")

# Pack additional RadioButtons
radio_button4.pack()
radio_button5.pack()

root.mainloop()

3. Customizing Tkinter RadioButtons:

Tkinter RadioButtons can be customized using various options like font, background, foreground, etc.

# ... (previous code)

# Customize RadioButtons
radio_button1.config(font=("Arial", 12), bg="lightblue", fg="black")

root.mainloop()

4. Binding actions to Tkinter RadioButtons:

You can bind functions to RadioButtons to perform actions when a RadioButton is selected.

# ... (previous code)

# Function to be called when a RadioButton is selected
def on_radio_select():
    print("Selected option:", selected_option.get())

# Bind the function to the RadioButtons
radio_button1.config(command=on_radio_select)
radio_button2.config(command=on_radio_select)
radio_button3.config(command=on_radio_select)

root.mainloop()

5. RadioButton variable in Tkinter:

The variable parameter in Radiobutton is used to associate the RadioButton with a Tkinter variable.

# ... (previous code)

# Access the variable
print("Current value:", selected_option.get())

root.mainloop()

6. Tkinter RadioButton value retrieval:

To retrieve the value of the selected RadioButton, you can use the get() method on the associated variable.

# ... (previous code)

# Access the variable
selected_value = selected_option.get()
print("Selected value:", selected_value)

root.mainloop()

7. RadioButton events in Tkinter:

Events such as <Button-1> (left mouse button click) can be bound to RadioButtons to trigger specific actions.

# ... (previous code)

# Function to be called on RadioButton click
def on_radio_click(event):
    print("RadioButton clicked!")

# Bind the function to the RadioButton event
radio_button1.bind("<Button-1>", on_radio_click)

root.mainloop()