CTkRadioButton: Radio Button In CustomTkinter

Hello, Pythonistas Welcome Back. Today we will see how to make a fully functional modern Radio Button in CustomTkinter.

We will use the CTkRadioButton Widget.

How Does CTkRadioButton Look?

Sample CTkRadioButton

Basic Code

This is how you can make a simple radio button in CustomTkinter (or CTk).

from customtkinter import CTk, CTkRadioButton, IntVar, CTkLabel

class App(CTk):
    def __init__(self):
        super().__init__()
        self.title("CTkRadioButton Example")
        self.geometry("300x200")

        # Create a variable to keep track of the radio buttons
        self.radio_var = IntVar()
        # Create the first radio button
        self.radio = CTkRadioButton(self, text="Option 1", variable= self.radio_var, value=1, command=self.radio_event)
        self.radio.pack(padx=20, pady=20)
        # Create another radio button
        self.radio1 = CTkRadioButton(self, text="Option 2", variable= self.radio_var, value=2, command=self.radio_event)
        self.radio1.pack(padx=20, pady=20)

    # Function to display the selected radio option.
    def radio_event(self):
        self.label = CTkLabel(self, text=f"{self.radio_var.get()} selected")
        self.label.pack()

app = App()
app.mainloop()

Like any other widget in CTk, it is created and then pushed to the window.

But, that’s not it. You need a StringVar or an IntVar to check its current value. You can’t use a normal Python Variable for this purpose.

The value should be unique for each radio button otherwise we won’t be able to know which one is selected.

Variable is needed because we need to group all the options under one head. And value is to give the value of that specific option(or button)

As it is like a button you can give it a command function to shoot when you select or deselect it.

It takes a compulsory argument master. This will specify where it will stay.

A Sample Modern Radio Button

We will make a gender selector with the help of CTkRadioButton.

Gender Selector Program CTkRadioButton

For this, we just need to:

  1. Create three radio buttons, group them under the same variable, and also provide them unique values.
  2. Now, whenever a button is selected the respective gender would be displayed on the window.
  3. We can either use conditional statements or use a string variable to do the same.

That’s it!

Complete Source Code(Click Here For Complete Source Code):
from customtkinter import CTk, CTkRadioButton, IntVar, CTkLabel
class App(CTk):
    def __init__(self):
        super().__init__()
        self.title("CTkRadioButton Example")
        self.geometry("350x300")

        # Create a variable to keep track of the radio buttons
        self.radio_var = IntVar()
        # Create radio button for female option
        self.female = CTkRadioButton(self, text="Female", variable= self.radio_var, value=0, command=self.radio_event)
        self.female.grid(row=0, column=0, padx=20, pady=20)
        # Create radio button for male option
        self.male = CTkRadioButton(self, text="Male", variable= self.radio_var, value=1, command=self.radio_event)
        self.male.grid(row=1, column=0, padx=20, pady=20)
        # Create radio button for other option
        self.other = CTkRadioButton(self, text="Other", variable= self.radio_var, value=2, command=self.radio_event)
        self.other.grid(row=2, column=0, padx=20, pady=20)
        # Create radio button for Pefer Not to Say option
        self.other = CTkRadioButton(self, text="Pefer Not to Say", variable= self.radio_var, value=3, command=self.radio_event)
        self.other.grid(row=3, column=0, padx=20, pady=20)

    # Function to display the selected radio
    def radio_event(self):
        dic = {0:"Female", 1:"Male", 2:"Other", 3:"Pefer Not to Say"}
        selected = self.radio_var.get()
        # Check if the label exists before removing it
        if hasattr(self, "label"):
            self.label.grid_forget()

        self.label = CTkLabel(self, text=f"{dic[selected]} selected")
        self.label.grid(row=4, column=0, )

app = App()
app.mainloop()

You can check out this amazing app made using CTkRadioButton.

All Configurations

argumentvalue
masterroot, tkinter.Frame or CTkFrame
widthwidth of complete widget in px
heightheight of complete widget in px
radiobutton_widthwidth of radiobutton in px
radiobutton_heightheight of radiobutton in px
corner_radiuscorner radius in px
border_width_uncheckedborder width in unchecked state in px
border_width_checkedborder width in checked state in px
fg_colorforeground (inside) color, tuple: (light_color, dark_color) or single color
border_colorborder color, tuple: (light_color, dark_color) or single color
hover_colorhover color, tuple: (light_color, dark_color) or single color
text_colortext color, tuple: (light_color, dark_color) or single color
text_color_disabledtext color when disabled, tuple: (light_color, dark_color) or single color
textstring
textvariableTkinter StringVar to control the text
fontbutton text font, tuple: (font_name, size)
hoverenable/disable hover effect: True, False
state“normal” (standard) or “disabled” (not clickable, darker color)
commandfunction will be called when the checkbox is clicked
variableTkinter variable to control or read checkbox state
valuestring or int value for variable when RadioButton is clicked

I am not going to give any challenges at the end of such articles as they are just for your quick reference.

Note I haven’t included all the methods and attributes. You can always get that on the documentation.

Leave a Reply