The CTkCheckBox is a checkbox widget that allows users to select or deselect options. It supports variables to control or retrieve the current state and is highly customizable.
Simple Example
import customtkinter
def checkbox_event():
print("Checkbox toggled, current value:", check_var.get())
# Initial setup
app = customtkinter.CTk()
app.geometry("400x300")
# Creating a variable to control the state
check_var = customtkinter.StringVar(value="on")
# Creating the checkbox
checkbox = customtkinter.CTkCheckBox(app, text="CTkCheckBox",
command=checkbox_event,
variable=check_var,
onvalue="on", offvalue="off")
checkbox.pack(pady=20)
app.mainloop()
CTkCheckBox Arguments
Argument | Description |
---|---|
master |
Parent widget: can be root, Frame, or CTkFrame. |
width |
Width of the entire widget in pixels. |
height |
Height of the entire widget in pixels. |
checkbox_width |
Width of the checkbox in pixels. |
checkbox_height |
Height of the checkbox in pixels. |
corner_radius |
Corner radius in pixels. |
border_width |
Border width in pixels. |
fg_color |
Main background color (single color or tuple: (light_color, dark_color)). |
border_color |
Border color (single color or tuple: (light_color, dark_color)). |
hover_color |
Color when hovering over the checkbox. |
text_color |
Text color (single color or tuple: (light_color, dark_color)). |
text_color_disabled |
Text color when disabled. |
text |
Text displayed next to the checkbox. |
textvariable |
StringVar object to control the text. |
font |
Font of the text (e.g., (font_name, size)). |
hover |
Enables/disables hover effect. Default: True . |
state |
Sets the checkbox state: tkinter.NORMAL or tkinter.DISABLED . |
command |
Function called when the state is toggled manually. |
variable |
StringVar or IntVar object to control the checkbox state. |
onvalue |
Value assigned to the variable when checked (can be string or int). |
offvalue |
Value assigned to the variable when unchecked (can be string or int). |
CTkCheckBox Methods
Method | Description |
.configure() |
Updates the widget properties (e.g., checkbox.configure(state="disabled") ). |
.cget(attribute) |
Retrieves the current value of an attribute (e.g., text = checkbox.cget("text") ). |
.get() |
Retrieves the current value (onvalue or offvalue ). |
.select() |
Activates the checkbox (sets the value to onvalue without triggering the command). |
.deselect() |
Deactivates the checkbox (sets the value to offvalue without triggering the command). |
.toggle() |
Toggles the current state, triggering the associated command. |
Example with Disabled State
import customtkinter
# Initial setup
app = customtkinter.CTk()
app.geometry("400x300")
# Creating a disabled checkbox
checkbox = customtkinter.CTkCheckBox(app, text="Disabled", state="disabled")
checkbox.pack(pady=20)
app.mainloop()
With these examples and details, you can customize and use CTkCheckBox in CustomTkinter to create more interactive interfaces!
Copyright Statement: Unless stated otherwise, all articles are original to this site, please credit the source when sharing.
Article link:http://pybeginners.com/customtkinter/ctkcheckbox-in-customtkinter/
License Agreement:Attribution-NonCommercial 4.0 International License