How to change the background color of a tkinter Canvas after it is created?
11,698
You can use the configure
method that exists on all widgets, and call it on the canvas
to modify the background
option:
You can change the background color by clicking on the canvas in the following example:
import tkinter as tk
def change_color(dummy_e):
canvas.configure(bg='cyan')
root = tk.Tk()
canvas = tk.Canvas(root, bg='red')
canvas.pack()
canvas.bind('<1>', change_color)
root.mainloop()
Author by
Connor
Updated on June 18, 2022Comments
-
Connor almost 2 years
I know that
canvas = tkinter.Canvas(bg='red')
creates a canvas with the background color of red. Since the background color is chosen when creating a new canvas, is there any way to change this later on in the program without creating a new canvas?The best solution I can think of is creating a big rectangle that covers the whole screen with the desired background color, but the actual background color remains the same after I do this and is revealed when I clear the canvas.