I want to create a program where I can toggle the color of many squares with a mouse click. I am able to create the shapes and toggle the color.
My problem is with many squares. My “change color” function toggles the color of ALL the squares simultaneously. I understand the reason is I am using the same tag but I do not know how to use many tags with the same function.
import tkinter
from tkinter import *
import math
from itertools import cycle
# Variables
shot_colors = ['red', 'blue']
scale = 4.04
radius = scale * 50
diameter = 2 * radius
chord_length = scale * 32.5
chord_angle = 37.93
chord_angle_compliment = 360 - chord_angle
x0 = 50
y0 = 50
x1 = x0 + diameter
y1 = y0 + diameter
start = 270 + 0.5 * chord_angle
# Define a function to change the state of the Widget
def change_color(*args, colors=cycle(shot_colors)):
canvas.itemconfig(tagOrId="shot", fill=next(colors))
root = Tk()
canvas = Canvas(root, width=1000, height=750)
# Create wafer shape, circle with flat oriented down
wafer = canvas.create_arc(x0, y0, x1, y1, start=start, extent=chord_angle_compliment, style=tkinter.CHORD, outline="black",
fill="gray", width=2)
# Create shot shapes, rectangle, this will be a for loop to create many shot(s)
shot1 = canvas.create_rectangle(257, 257, 277, 277, outline="black", fill="blue", tag="shot")
shot2 = canvas.create_rectangle(277, 277, 297, 297, outline="black", fill="blue", tag="shot")
# Change color of shot when clicked with mouse
canvas.tag_bind("shot", "<Button-1>", change_color)
canvas.pack()
root.mainloop()
New contributor
Evan Jones is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.