Overview
CTkSwitch is a toggle switch widget with modern iOS-style styling. It provides an intuitive on/off control with smooth visual feedback and support for variable binding.
Constructor
CTkSwitch(
master,
width=100,
height=24,
switch_width=36,
switch_height=18,
corner_radius=None,
border_width=None,
button_length=None,
bg_color="transparent",
fg_color=None,
border_color="transparent",
progress_color=None,
button_color=None,
button_hover_color=None,
text_color=None,
text_color_disabled=None,
text="CTkSwitch",
font=None,
textvariable=None,
onvalue=1,
offvalue=0,
variable=None,
hover=True,
command=None,
state=tkinter.NORMAL,
**kwargs
)
Parameters
Total width of the switch widget including label
Height of the switch widget
Width of the switch button
Height of the switch button
corner_radius
Optional[int]
default:"None"
Corner radius of the switch. If None, uses theme default
border_width
Optional[int]
default:"None"
Border width of the switch. If None, uses theme default
button_length
Optional[int]
default:"None"
Length of the sliding button. If None, uses theme default
bg_color
Union[str, Tuple[str, str]]
default:"'transparent'"
Background color behind the widget
fg_color
Optional[Union[str, Tuple[str, str]]]
default:"None"
Foreground color of the switch track. If None, uses theme default
border_color
Union[str, Tuple[str, str]]
default:"'transparent'"
Border color of the switch
progress_color
Optional[Union[str, Tuple[str, str]]]
default:"None"
Color of the progress/filled part when switch is on. If None, uses theme default
button_color
Optional[Union[str, Tuple[str, str]]]
default:"None"
Color of the sliding button. If None, uses theme default
button_hover_color
Optional[Union[str, Tuple[str, str]]]
default:"None"
Button color when hovering. If None, uses theme default
text_color
Optional[Union[str, Tuple[str, str]]]
default:"None"
Color of the label text. If None, uses theme default
text_color_disabled
Optional[Union[str, Tuple[str, str]]]
default:"None"
Text color when disabled. If None, uses theme default
Label text displayed next to the switch
font
Optional[Union[tuple, CTkFont]]
default:"None"
Font for the label text
textvariable
Union[tkinter.Variable, None]
default:"None"
Tkinter variable to link with the label text
onvalue
Union[int, str]
default:"1"
Value when switch is on
offvalue
Union[int, str]
default:"0"
Value when switch is off
variable
Union[tkinter.Variable, None]
default:"None"
Tkinter variable to track switch state
Enable/disable hover effect
command
Union[Callable, Any]
default:"None"
Callback function called when switch is toggled. Takes no arguments
state
str
default:"tkinter.NORMAL"
State of the widget: tkinter.NORMAL or tkinter.DISABLED
Methods
get()
Returns the current value (onvalue if on, offvalue if off).
Returns: Union[int, str] - The onvalue or offvalue
select()
Turns the switch on (sets to onvalue).
deselect()
Turns the switch off (sets to offvalue).
toggle()
Toggles the switch state and calls the command callback.
Configures widget parameters.
switch.configure(text="New label", fg_color="blue")
cget()
Returns the value of a configuration parameter.
current_state = switch.cget("state")
Usage Examples
Basic Switch
import customtkinter as ctk
root = ctk.CTk()
switch = ctk.CTkSwitch(root, text="Dark Mode")
switch.pack(pady=20, padx=20)
root.mainloop()
Switch with Command
def on_switch_toggle():
if switch.get() == 1:
print("Switch is ON")
else:
print("Switch is OFF")
switch = ctk.CTkSwitch(
root,
text="Enable Notifications",
command=on_switch_toggle
)
switch.pack(pady=20)
Using with Variable
import tkinter as tk
# Create variable to track state
notifications_var = tk.IntVar(value=1)
switch = ctk.CTkSwitch(
root,
text="Notifications",
variable=notifications_var,
onvalue=1,
offvalue=0
)
switch.pack(pady=20)
# Access state through variable
def save_settings():
if notifications_var.get() == 1:
print("Notifications enabled")
else:
print("Notifications disabled")
save_btn = ctk.CTkButton(root, text="Save", command=save_settings)
save_btn.pack()
Programmatically Control Switch
switch = ctk.CTkSwitch(root, text="Auto-save")
switch.pack(pady=20)
# Turn switch on
switch.select()
# Turn switch off
switch.deselect()
# Toggle switch
switch.toggle()
# Check current state
if switch.get() == 1:
print("Switch is on")
Custom Styled Switch
switch = ctk.CTkSwitch(
root,
text="Premium Feature",
switch_width=48,
switch_height=24,
corner_radius=12,
button_length=20,
fg_color="#95a5a6",
progress_color="#2ecc71",
button_color="white",
button_hover_color="#ecf0f1",
text_color="#2c3e50",
font=("Helvetica", 14, "bold")
)
switch.pack(pady=20, padx=20)
Multiple Switches for Settings
import tkinter as tk
settings = {
"Auto-save": tk.IntVar(value=1),
"Dark mode": tk.IntVar(value=0),
"Show notifications": tk.IntVar(value=1),
"Enable sounds": tk.IntVar(value=0)
}
for setting, var in settings.items():
switch = ctk.CTkSwitch(
root,
text=setting,
variable=var
)
switch.pack(pady=10, padx=20, anchor="w")
def apply_settings():
for setting, var in settings.items():
status = "enabled" if var.get() == 1 else "disabled"
print(f"{setting}: {status}")
apply_btn = ctk.CTkButton(root, text="Apply Settings", command=apply_settings)
apply_btn.pack(pady=20)
Switch with String Values
import tkinter as tk
mode_var = tk.StringVar(value="light")
switch = ctk.CTkSwitch(
root,
text="Theme",
variable=mode_var,
onvalue="dark",
offvalue="light"
)
switch.pack(pady=20)
def apply_theme():
theme = mode_var.get()
print(f"Applying {theme} theme")
apply_btn = ctk.CTkButton(root, text="Apply", command=apply_theme)
apply_btn.pack()
Pre-selected Switch
# Create switch in ON state
switch = ctk.CTkSwitch(root, text="Feature enabled")
switch.select() # Turn it on
switch.pack(pady=20)
Disabled Switch
switch = ctk.CTkSwitch(
root,
text="Premium Only (Disabled)",
state=ctk.DISABLED
)
switch.select() # Can pre-select even when disabled
switch.pack(pady=20)