Tkinter Radio Button Customization: Font, Text, and Groups

In this tutorial, you’ll learn how to adjust Tkinter radio button font sizes, disable and change their text, and even create multiple groups of radio buttons.

 

 

Adjust Radio Button Font Size

You can adjust the radio button font by setting the font option in the ttk style:

import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.title("Radio Button Customization")
options = ["Plan A", "Plan B", "Plan C"]
selected_option = tk.StringVar()
def display_choice():
    print("Selected:", selected_option.get())
for option in options:
    radio_button = ttk.Radiobutton(root, text=option, value=option, 
                                   variable=selected_option, command=display_choice)
    radio_button.pack()
style = ttk.Style()
style.configure("TRadiobutton", font=('Helvetica', 14))
root.mainloop()

Output:

Adjust Radio Button Font Size

 

Disable Radio Button

You can disable a radio button by setting its state property to ‘disabled’:

import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.title("Disable Radio Button")
options = ["Plan D", "Plan E", "Plan F"]
selected_option = tk.StringVar(value="Plan D")
radio_buttons = []
for option in options:
    radio_button = ttk.Radiobutton(root, text=option, value=option, 
                                   variable=selected_option)
    radio_button.pack()
    radio_buttons.append(radio_button)

# Disable the second radio button
radio_buttons[1]['state'] = 'disabled'
root.mainloop()

Output:

Disable Radio Button

 

Change Radio Button Text

You can change the text of a radio button using the config method:

import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.title("Change Radio Button Text")
options = ["Basic Plan", "Standard Plan", "Premium Plan"]
selected_option = tk.StringVar(value=options[0])
def update_text():
    radio_buttons[0].config(text="Updated Plan")
radio_buttons = []
for option in options:
    radio_button = ttk.Radiobutton(root, text=option, value=option, 
                                   variable=selected_option)
    radio_button.pack()
    radio_buttons.append(radio_button)

# Button to trigger text update
update_button = ttk.Button(root, text="Update Text", command=update_text)
update_button.pack()
root.mainloop()

 

Add Borders to Radio Buttons

You can use a Tkinter Frame with a defined border to wrap. Then you can set the borderwidth for it:

import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.title("Add Borders to Radio Buttons")
options = ["Silver Package", "Gold Package", "Diamond Package"]
selected_option = tk.StringVar(value=options[0])
for option in options:
    radio_button_frame = tk.Frame(root, borderwidth=2, relief="solid")
    radio_button_frame.pack(pady=5)
    radio_button = ttk.Radiobutton(radio_button_frame, text=option, value=option, 
                                   variable=selected_option)
    radio_button.pack(padx=10, pady=5)
root.mainloop()

Output:

Add Borders to Radio Buttons

 

Create Multiple Radio Button Groups

Multiple groups of radio buttons are required for cases where independent choices are required from different sets of options.

You can create separate groups of radio buttons using LabelFrame widgets to ensure that selections in one group do not affect the other.

import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.title("Multiple Radio Button Groups")
plans = ["Plan G", "Plan H", "Plan I"]
plan_var = tk.StringVar(value=plans[0])

features = ["Feature A", "Feature B", "Feature C"]
feature_var = tk.StringVar(value=features[0])

def display_selections():
    print(f"Selected Plan: {plan_var.get()}, Selected Feature: {feature_var.get()}")

plan_frame = ttk.LabelFrame(root, text="Service Plans")
plan_frame.pack(padx=10, pady=10, fill='x', expand=True)
for plan in plans:
    plan_rb = ttk.Radiobutton(plan_frame, text=plan, value=plan, variable=plan_var)
    plan_rb.pack(anchor='w')

feature_frame = ttk.LabelFrame(root, text="Additional Features")
feature_frame.pack(padx=10, pady=10, fill='x', expand=True)
for feature in features:
    feature_rb = ttk.Radiobutton(feature_frame, text=feature, value=feature, variable=feature_var)
    feature_rb.pack(anchor='w')
selection_button = ttk.Button(root, text="Show Selections", command=display_selections)
selection_button.pack(pady=10)
root.mainloop()

Output:

Create Multiple Radio Button Groups

Leave a Reply

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