Choose Your Hobbies: CheckBoxes In CustomTkinter Python

Hello, Pythonistas Welcome Back. Today we will see how to make modern checkboxes in Python CustomTkiner.

We will use the CTkCheckBox Widget.

We will make a simple hobby selector for showcasing its use case.

How Does It Look?

checkboxes

Basic Code

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

# Create a custom application class "App" that inherits from CTk (Custom Tkinter)
class App(CTk):
    # Constructor of the class
    def __init__(self):
        # Call the constructor of the parent class (CTk) using super()
        super().__init__()

        # Create a StringVar to hold the value (initially empty)
        self.check_var = StringVar()
        # Create a CTkCheckBox widget with text "Switch"
        # When the checkbox is toggled, the self.checkbox_event method will be called
        # The checkbox's variable is self.check_var, and its values are "on" (checked) and "off" (unchecked)
        self.checkbox = CTkCheckBox(self, text="Switch", command=self.checkbox_event,
                                            variable=self.check_var, onvalue="on", offvalue="off")
        # Pack the checkbox widget, adding padding around it
        self.checkbox.pack(padx=10, pady=10)

    # Method to handle the checkbox toggle event
    def checkbox_event(self):
        # Create a new CTkLabel widget and display the current value of the checkbox
        label = CTkLabel(self, text=f"checkbox toggled, current value: {self.check_var.get()}")
        label.pack()

# Create an instance of the custom application class "App"
app = App()
app.mainloop()

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

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

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

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

A Sample Modern CTkCheckBox

We will see how we can make a simple program that displays selected hobbies.

hobby selector using CTkCheckBox in CustomTkinter Python

We would need:

  • A label to let the user know what to do
  • A frame to hold checkboxes
    • 3 checkboxes (You can have more or less)
    • A label to display selected hobbies.

Simple that’s all!

# Create a custom application class "Hobbies" that inherits from CTk (Custom Tkinter)
class Hobbies(CTk):
    # Constructor of the class
    def __init__(self):
        # Call the constructor of the parent class (CTk) using super()
        super().__init__()
        self.title("Hobbies")

        # Create a label to display "Select your hobbies"
        self.display_label = CTkLabel(self, text="Select your hobbies")
        self.display_label.pack(padx=10, pady=10)

        # Create a frame to hold the checkboxes
        self.frame =CTkFrame(self)
        self.frame.pack(padx=70, pady=10)

        # Create a StringVar to store the value of the first hobby (initially empty)
        self.hobby1 = StringVar()
        # Create a CTkCheckBox for "Coding"
        # When the checkbox is toggled, the self.hobby() method will be called
        # The checkbox's variable is self.hobby1, and its values are "Coding" (checked) and "" (unchecked)
        self.hobby1_cb = CTkCheckBox(self.frame, text="Coding", command=self.hobby,
                                            variable=self.hobby1, onvalue="Coding", offvalue="")
        self.hobby1_cb.grid(row=0, padx=10, pady=10)

        # Create a StringVar to store the value of the second hobby (initially empty)
        self.hobby2 = StringVar()
        # Create a CTkCheckBox for "Cricket"
        # When the checkbox is toggled, the self.hobby() method will be called
        # The checkbox's variable is self.hobby2, and its values are "Cricket" (checked) and "" (unchecked)
        self.hobby2_cb = CTkCheckBox(self.frame, text="Cricket", command=self.hobby,
                                            variable=self.hobby2, onvalue="Cricket", offvalue="")
        self.hobby2_cb.grid(row=1, padx=10, pady=10)

        # Create a StringVar to store the value of the third hobby (initially empty)
        self.hobby3 = StringVar()
        # Create a CTkCheckBox for "Drawing"
        # When the checkbox is toggled, the self.hobby() method will be called
        # The checkbox's variable is self.hobby3, and its values are "Drawing" (checked) and "" (unchecked)
        self.hobby3_cb = CTkCheckBox(self.frame, text="Drawing", command=self.hobby,
                                            variable=self.hobby3, onvalue="Drawing", offvalue="")
        self.hobby3_cb.grid(row=2, padx=10, pady=10)

        # Create a label to display the selected hobbies
        self.label= CTkLabel(self.frame, text="")
        self.label.grid(row=3)

    # Method to handle the checkbox toggle event
    def hobby(self):
        # Destroy the existing label to clear its contents
        self.label.destroy()

        # Create a new label to display the selected hobbies
        self.label = CTkLabel(self.frame, text=f"{self.hobby1.get()} {self.hobby2.get()} {self.hobby3.get()}")
        self.label.grid(row=3)

# Create an instance of the custom application class "Hobbies"
app = Hobbies()
# Start the main event loop of the application
app.mainloop()

You can also add these configurations using the configure() method of CTkCheckBox. Provide attributes in attribute=”value”. Only if the value will be a string.

All Configurations

Here are a few configurations you can add:

argumentvalue
masterWhere to place it. (window, or frame)
widthwidth of complete widget (in px)
heightheight of complete widget (in px)
fg_colorcolor of the box
border_colorcolor of box’s border
hover_colorcolor of box when you hover over it
text_colorcolor of the text displayed on its side
text_color_disabledcolor of text when you have disabled the box. Which means it can no longer be selected
textvariableThe variable we defined here to keep track of checkbox state. (This can keep track of StringVar only)
hoverto decide whether you want to enable hover effects or not. Set True (default) if yes else False.
stateTo make the box selectable or not. NORAML(default) for allowing selection and DISABLED for not allowing selection.
commandfunction will be called when the box or text on its side is clicked
variableIt is used to keep track of the state. (on or off) just like textvariable. But supports IntVar too.
onvaluestring or int for variable in checked state
offvaluestring or int for variable in unchecked state

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

This Post Has 5 Comments

  1. canada

    whoah this blog is fantastic i like reading your articles. Keep up the great work! You realize, lots of people are hunting around for this information, you could help them greatly.

  2. best gold ira companies

    My brother recommended I might like this blog.
    He was totally right. This post actually made my day.
    You cann’t imagine simply how much time I had spent for this information! Thanks!

  3. Maitry

    thanks a lot!

  4. Oliver

    I likewise believe thus, perfectly composed post! .

  5. Maitry

    Thank you!