如何检测点击图片?

时间:2018-02-24 21:03:07

标签: python python-3.x tkinter tkinter-canvas

伙计们,我目前正在使用Tkinter在Python中制作国际象棋,并想知道如何检测图像是否被点击?

此代码为我存储在字典中的每个图像添加了一个单击功能

def MouseClickOnEachPiece(self, rows, columns, event): 

   global AllPieces

   while True:
     for ThePieces in AllPieces:
         if board[rows][columns] == ThePieces:
             self.canvas.bind('<Button-1>', MakeMove)
         elif board[rows][columns] != ThePieces:
             continue 

此代码用于移动

def MakeMove(self, rows, columns, event):

    global AllPieces

    print("You have clicked at: ", event.x , event.y)

    while True:
        for i in AllPieces:
            if #the image is clicked : # -- What would i use to check if the image is clicked?
                canvas.move(i, #themove )

1 个答案:

答案 0 :(得分:0)

如果可能,您可以使用按钮并在按钮上放置图像。

from tkinter import *
from tkinter import ttk

root = Tk()

button = ttk.Button(root)
button.pack()

theImage = PhotoImage(file = "source")

button.config(image = theImage)