The Canvas widget is one of the most versatile widgets in the Tkinter Library. It is used for creating shapes of different types and sizes, animating objects, visualizing graphics, and many more. To change the property of a particular item in Tkinter, we can use itemconfig(**options) method. It takes options such as background color, outline color, and other useful properties of the items defined in a canvas.
Example
In this example, we will create a rectangle such that the color inside the rectangle would change after clicking a Button.
# Import the required libraries from tkinter import * from tkinter import ttk # Create an instance of Tkinter Frame win = Tk() # Set the geometry win.geometry("700x300") # Define a function to change the state of the Widget def change_color(): canvas.itemconfig(rectangle, fill='green') # Define a Canvas Widget canvas = Canvas(win, width=500, height=240) canvas.pack() # Create a rectangle in Canvas rectangle = canvas.create_rectangle(100, 100, 400, 400, fill='blue') # Create a Button to Disable the Combobox Widget ttk.Button(win, text="Change Color", command=change_color).pack() win.mainloop()
Output
When you run the above code, it will display a window with a blue-colored rectangle at the center.
Now, click the "Change Color" button. It will change the color of the rectangle to green.