Skip to content

CTkSwitch

Tom Schimansky edited this page Dec 5, 2022 · 12 revisions

The switch should behave similar to the CTkCheckBox.

Example Code:

Default theme:

switch_var = customtkinter.StringVar(value="on")

def switch_event():
    print("switch toggled, current value:", switch_var.get())

switch_1 = customtkinter.CTkSwitch(master=root_tk, text="CTkSwitch", command=switch_event,
                                   variable=switch_var, onvalue="on", offvalue="off")
switch_1.pack(padx=20, pady=10)

Arguments:

argument value
master master widget
width width of complete widget in px
height height of complete widget in px
switch_width width of switch in px
switch_height height of switch in px
corner_radius corner radius in px
border_width box border width in px
fg_color foreground (inside) color, tuple: (light_color, dark_color) or single color
border_color border color, tuple: (light_color, dark_color) or single color or "transparent" default is "transparent"
progress_color color of switch when enabled, tuple: (light_color, dark_color) or single color or "transparent"
button_color color of button, tuple: (light_color, dark_color) or single color
button_hover_color hover color of button, tuple: (light_color, dark_color) or single color
hover_color hover color, tuple: (light_color, dark_color) or single color
text_color text color, tuple: (light_color, dark_color) or single color
text string
textvariable Tkinter StringVar to control the text
font button text font, tuple: (font_name, size)
command function will be called when the checkbox is clicked or .toggle() is called
variable Tkinter variable to control or read checkbox state
onvalue string or int for variable in checked state
offvalue string or int for variable in unchecked state
state "normal" (standard) or "disabled" (not clickable)

Methods:

  • .configure(attribute=value, ...)

    All attributes can be configured and updated.

    ctk_switch.configure(fg_color=..., progress_color=..., command=..., text=..., ...)
  • .get()

    Get current value of switch, 1 or 0 (checked or not checked).

  • .select()

    Turn on switch (set value to 1), command will not be triggered.

  • .deselect()

    Turn off switch (set value to 0), command will not be triggered.

  • .toggle()

    Flip current value of switch, command will be triggered.

⚠️ Attention ⚠️

The Github Wiki is outdated, the new documentation can be found at:

https://customtkinter.tomschimansky.com

Clone this wiki locally