Question

How should I write the code so that when I click on any circle with the same mouse button, the circle turns blue?

Windows, PyCharm, Python, tkinter, Canvas, program question There are three circles in the tkinter Canvas code with different colors. I want it to call the recolor method when I click on a circle. Unfortunately, I just can’t specify the If else conditions. I want to click with the same mouse button. The first round is ok, but the rest of the rounds are not! How should I write the code so that when I click on any circle with the same mouse button, the circle turns blue?


import tkinter as tk

class CanvasEvents(tk.Tk):
    def __init__(self):
        super().__init__()
        self.geometry('510x510+700+100')
        self.title("Event")

        self.canvas = tk.Canvas(self, bg='grey', width=500, height=500)
        self.canvas.pack(pady=5)

        kor1 = self.canvas.create_oval(20, 20, 100, 100, width=2, fill="orange")
        kor2 = self.canvas.create_oval(220, 20, 300, 100, width=2, fill="red")
        kor3 = self.canvas.create_oval(420, 20, 500, 100, width=2, fill="green")

        self.canvas.tag_bind(kor1, '<Button-1>', self.object_click_event)
        self.canvas.tag_bind(kor2, '<Button-1>', self.object_click_event)
        self.canvas.tag_bind(kor3, '<Button-1>', self.object_click_event)

    def object_click_event(self, event):
        # If else ?????????????????????
        self.canvas.itemconfigure(event.num, fill="blue")
        print('Clicked object at: ', self.canvas.coords(event.num), event.num)


if __name__ == "__main__":
    ablak = CanvasEvents()
    ablak.mainloop()
    

Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

KFSys
Site Moderator
Site Moderator badge
July 31, 2023

Hey @tvcsop,

You saw your comment about the code being legit, having said that, I think you need to adjust the object_click_event method to change the color of the clicked item. You can access the clicked item using the tag_closest method and then change its color using itemconfigure.

Something like this one here:

import tkinter as tk

class CanvasEvents(tk.Tk):
    def __init__(self):
        super().__init__()
        self.geometry('510x510+700+100')
        self.title("Event")

        self.canvas = tk.Canvas(self, bg='grey', width=500, height=500)
        self.canvas.pack(pady=5)

        self.canvas.create_oval(20, 20, 100, 100, width=2, fill="orange", tags="kor1")
        self.canvas.create_oval(220, 20, 300, 100, width=2, fill="red", tags="kor2")
        self.canvas.create_oval(420, 20, 500, 100, width=2, fill="green", tags="kor3")

        self.canvas.tag_bind("kor1", '<Button-1>', self.object_click_event)
        self.canvas.tag_bind("kor2", '<Button-1>', self.object_click_event)
        self.canvas.tag_bind("kor3", '<Button-1>', self.object_click_event)

    def object_click_event(self, event):
        closest = self.canvas.find_closest(event.x, event.y)  # find closest item to the clicked point
        self.canvas.itemconfigure(closest, fill="blue")  # change color of the clicked item
        print('Clicked object at: ', self.canvas.coords(closest), closest)

if __name__ == "__main__":
    ablak = CanvasEvents()
    ablak.mainloop()

In the create_oval method, I added a tags parameter which can be used to bind the click event for each circle. The object_click_event method uses find_closest to find the closest item to the clicked point. It then uses itemconfigure to change the color of the clicked item to blue.

The code is legible, I forgot to enter it! https://www.onlinegdb.com/ODwJiVxOY

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel