In this video I’ll show you how to use the Switch Widget with CustomTkinter and Python.

The switch is exactly what it sounds like…a switch that switches from on to off.

I’ll show you how to code it, and also how to customize it in this video.

Python Code: ctk_switch.py
(Github Code)

from tkinter import *
import customtkinter

customtkinter.set_appearance_mode("dark")  # Modes: system (default), light, dark
customtkinter.set_default_color_theme("dark-blue")  # Themes: blue (default), dark-blue, green

#root = Tk()
root = customtkinter.CTk()

root.title('Tkinter.com - Custom Tkinter Switch')
root.iconbitmap('images/codemy.ico')
root.geometry('700x300')

# Create Function
def switcher():
	my_label.configure(text=switch_var.get())

# Create Toggle function
def clicker():
	#my_switch.deselect()
	#my_switch.select()
	my_switch.toggle()

# Create a StringVar
switch_var = customtkinter.StringVar(value="on")
# Create Switch
my_switch = customtkinter.CTkSwitch(root, text="Switch", command=switcher,
	variable=switch_var, onvalue="on", offvalue="off",
	#width=200,
	#height=100,
	switch_width=200,
	switch_height=25,
	#corner_radius=10,
	border_color="orange",
	border_width=5,
	fg_color="red",
	progress_color="green",
	button_color="pink",
	button_hover_color="yellow",
	font=("Helvetica", 24),
	text_color="blue",
	state="normal",

	)
my_switch.pack(pady=40)


# Create a label
my_label = customtkinter.CTkLabel(root, text="")
my_label.pack(pady=10)

# Create Button
my_button = customtkinter.CTkButton(root, text="Click Me!", command=clicker)
my_button.pack(pady=10)

root.mainloop()



John Elder

John is the CEO of Codemy.com where he teaches over 100,000 students how to code! He founded one of the Internet's earliest advertising networks and sold it to a publicly company at the height of the first dot com boom. After that he developed the award-winning Submission-Spider search engine submission software that's been used by over 3 million individuals, businesses, and governments in over 42 countries. He's written several Amazon #1 best selling books on coding, and runs a popular Youtube coding channel.

View all posts

Add comment

Your email address will not be published. Required fields are marked *

John Elder

John is the CEO of Codemy.com where he teaches over 100,000 students how to code! He founded one of the Internet's earliest advertising networks and sold it to a publicly company at the height of...