Python 如何检测点击图像?
伙计们,我现在正在用Tkinter用Python下国际象棋,想知道如何检测图像是否被点击 这段代码为我存储在字典中的每个图像添加了一个click函数Python 如何检测点击图像?,python,python-3.x,tkinter,tkinter-canvas,Python,Python 3.x,Tkinter,Tkinter Canvas,伙计们,我现在正在用Tkinter用Python下国际象棋,想知道如何检测图像是否被点击 这段代码为我存储在字典中的每个图像添加了一个click函数 def MouseClickOnEachPiece(self, rows, columns, event): global AllPieces while True: for ThePieces in AllPieces: if board[rows][columns] == ThePieces:
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
如果可能,您可以使用按钮并在按钮上放置图像
from tkinter import *
from tkinter import ttk
root = Tk()
button = ttk.Button(root)
button.pack()
theImage = PhotoImage(file = "source")
button.config(image = theImage)
如果我在按钮上使用.move功能,该按钮是否能够随图片移动?完成此操作后,您的图像具有透明部分,它显示为白色,而不是背景颜色。我想知道有什么方法可以使按钮透明,这样你就可以看到棋盘上的图像。当然,对于棋盘,你所要做的就是检查点击位置?一个简单的计算应该会告诉你确切的线路板位置。画布上有一个
bind
方法,用于绑定画布上各个项目的所有绑定。这个方法是有文档记录的,在这个站点上有很多关于画布对象绑定的问题和答案。
from tkinter import *
from tkinter import ttk
root = Tk()
button = ttk.Button(root)
button.pack()
theImage = PhotoImage(file = "source")
button.config(image = theImage)