pythontkintertkinter-canvas

How to know if my mouse is over an object?


I am creating a game where if you click a square it changes colors. I want to make the player able to choose the size of his gameboard. To do that I want to use triangles. I am using create_polygon(x,y,...) and I know it has a widget activedash. I was wondering is there any way to get recieve the activedash signal somehow if the player clicks on the object.

I tried the folowing

if  väiksemk.tk.active==True:
    print("works")

but it didn't work.

Here is my code

from tkinter import*
import time
raam = Tk()
tahvel = Canvas(raam, width=700, height=700, background="wheat")
tahvel.grid()
raam.resizable(0,0)

tahvel.create_rectangle(450,375,250,325,fill="green")
tahvel.create_text(350,350,text="alusta",fill="black")
tahvel.create_text(175,470,text="mängulaua laius", fill="black",font=(30))
tahvel.create_text(175,490,text="mängulaua kõrgus", fill="black",font=(30))
väiksemk=tahvel.create_polygon(250,470,270,480,270,460,fill="black",activefill="white")    

klõps=0
värv=[[0 for i in range(25)]for i in range(25)]
värv2=[[0 for i in range(25)]for i in range(25)]
laius=25
kõrgus=25
    
def hiir(event):
    global klõps
    if klõps==0:
        if event.x<450 and event.x>250 and event.y<375 and event.y>325:
            tahvel.delete("all")
            klõps=1
            ruudustik()
        else:
            0
    elif klõps==1:
        x=event.x
        y=event.y
        värvi(x,y)

def ruudustik():
    a=100
    b=0
    global laius
    global kõrgus
    while a<laius*20+120:
        tahvel.create_line(a,b,a,laius*20+100)
        a+=20
    a=0
    b=100
    while b<kõrgus*20+120:
        tahvel.create_line(a,b,kõrgus*20+100,b)
        b+=20
        
    a=100
    b=0
    while a<laius*20+120:
        tahvel.create_line(a,b,a,laius*20+100,width=5)
        a+=100
    a=0
    b=100
    while b<kõrgus*20+120:
        tahvel.create_line(a,b,kõrgus*20+100,b,width=5)
        b+=100
    

def värvi(x,y):
    a=0
    if x<100 or y<100 or x>600 or y>600:
        0
    else:
        if värv[x//20-5][y//20-5]==0:
            värv2[x//20-5][y//20-5]=tahvel.create_rectangle(x//20*20,y//20*20,x//20*20+20,y//20*20+20, fill="black")
            värv[x//20-5][y//20-5]=1
        elif värv[x//20-5][y//20-5]==1:
            tahvel.delete(värv2[x//20-5][y//20-5])
            värv[x//20-5][y//20-5]=0
            
            
raam.bind_all("<1>", hiir)
raam.mainloop()
    

It is in estonian if you want to understand the text and variable names. Thanks for your help.


Solution

  • One way to find any object in Canvas is to bind the canvas to 'Motion'. Then use a simple callback function to extract the information from the object. The following code demonstrates one way this can be achieved.

    import tkinter as tk
    
    app = tk.Tk()
    main = tk.Canvas(app)
    main.grid(sticky = tk.NSEW)
    
    def callback(event):
        obj = main.itemcget("current", "fill")
        if obj:
            x, y = event.x, event.y
            print(obj, main.type("current"), main.find_closest(x,y))
    
    main.bind("<Motion>", callback)
    
    A = main.create_rectangle(20, 20, 100, 100, fill = "red")
    B = main.create_oval(120, 20, 100, 100, fill = "yellow")
    
    app.mainloop()