How do I use a Canvas in CustomTkinter?
CustomTkinter does have a CTkCanvas, but it is a plain tk.Canvas that the library uses internally to draw its rounded widgets. It takes bg rather than fg_color, has a focus border, and knows nothing about light or dark mode, so you set those yourself:
import customtkinter as ctk
ctk.set_appearance_mode("dark")
app = ctk.CTk()
app.geometry("400x300")
def window_colour():
light, dark = ctk.ThemeManager.theme["CTk"]["fg_color"]
return dark if ctk.get_appearance_mode() == "Dark" else light
# CTkCanvas is a plain tk.Canvas: give it a background, drop the border
canvas = ctk.CTkCanvas(app, bg=window_colour(), highlightthickness=0)
canvas.pack(fill="both", expand=True, padx=10, pady=10)
canvas.create_oval(40, 40, 140, 140, fill="#1f6aa5", outline="")
app.mainloop() CTkCanvas or tk.Canvas?
They are the same widget. CTkCanvas subclasses tk.Canvas and adds only the helpers CustomTkinter needs to draw anti-aliased corners. Use whichever reads better in your code. Every create_line, create_text, bind and delete call from a tkinter tutorial works unchanged.
What it does not take are the CustomTkinter options. fg_color and corner_radius raise TclError: unknown option "-fg_color", because the canvas hands every option straight to Tk. That is also why .config() works on it, although CustomTkinter's own widgets insist on .configure().
Why it shows up as a grey box with a border
Left alone, the canvas uses the operating system's default window colour, which is light grey or white whatever the CustomTkinter appearance mode. It also has a focus highlight a few pixels wide. In a dark window that reads as a pale rectangle with an outline. bg plus highlightthickness=0 fixes both.
The theme colours are pairs of light and dark values, kept in ctk.ThemeManager.theme. Use the "CTk" entry when the canvas sits on the window itself, and "CTkFrame" when it sits inside a frame, because frames are a shade different from the window in both modes.
Following a light/dark switch
Calling set_appearance_mode later, or the system switching to dark mode while the app is open, recolours every CustomTkinter widget but not the canvas. CustomTkinter tells its own widgets through AppearanceModeTracker, and you can register the canvas the same way.
This hook is not in the CustomTkinter documentation, so treat it as internal. It is the same in 5.2 and 6.0, and the tracker ignores errors from callbacks, so a canvas destroyed later will not crash the app.
def follow_theme(mode): # called with "Light" or "Dark"
canvas.configure(bg=window_colour())
ctk.AppearanceModeTracker.add(follow_theme, canvas)
ctk.set_appearance_mode("light") # the canvas now followsImages: CTkImage will not draw on a canvas
canvas.create_image(..., image=CTkImage(...)) fails with TclError: image "<customtkinter...CTkImage object at ...>" doesn't exist. A CTkImage is a pair of Pillow images that only CustomTkinter widgets know how to show. A canvas needs a Tk image, so convert with Pillow's ImageTk.PhotoImage, and keep a reference to it. tkinter does not keep one, and an image nothing refers to is garbage collected and disappears from the canvas.
from PIL import Image, ImageTk
photo = ImageTk.PhotoImage(Image.open("logo.png").resize((64, 64)))
canvas.create_image(10, 10, image=photo, anchor="nw")
canvas.photo = photo # keep a reference, or the image vanishesScrolling and fonts do work
A CTkScrollbar drives a canvas exactly as a tkinter scrollbar does: point its command at the canvas view, point the canvas's yscrollcommand back at the scrollbar, and set a scrollregion bigger than the visible area. CTkFont is a real tkinter font, so create_text accepts it.
canvas = ctk.CTkCanvas(app, width=300, height=200, highlightthickness=0)
scrollbar = ctk.CTkScrollbar(app, command=canvas.yview)
canvas.configure(yscrollcommand=scrollbar.set, scrollregion=(0, 0, 300, 1000))
canvas.grid(row=0, column=0)
scrollbar.grid(row=0, column=1, sticky="ns")
canvas.create_text(150, 20, text="Scroll down", font=ctk.CTkFont(size=16, weight="bold"))High-DPI screens: the canvas does not scale
CustomTkinter enlarges its widgets on scaled displays and when you call set_widget_scaling. With scaling at 2.0, a button 100 wide is drawn 200 pixels wide, while a canvas 100 wide stays at 100, and so do the coordinates of everything drawn on it. If your drawing has to line up with the widgets around it, multiply by the same factor CustomTkinter uses.
s = ctk.ScalingTracker.get_widget_scaling(canvas) # 1.0, 1.25, 1.5 ...
canvas.configure(width=300 * s, height=200 * s)
canvas.create_rectangle(10 * s, 10 * s, 110 * s, 60 * s, fill="#1f6aa5") Doing it without the lookup
The Canvas is in the palette for CustomTkinter projects. The export writes ctk.CTkCanvas with the background from the inspector and highlightthickness=0 already set. It also fixes the appearance mode to light or dark to match the project background, so the canvas colour you picked still matches on a machine set to the other mode.
This example is ready to open: change it on the canvas and watch the Python update.
Open this example in the designer →