伙计们,我目前正在使用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 )
答案 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)