首页 > 解决方案 > 向变量添加坐标

问题描述

我正在尝试创建一个程序,您可以在其中尽可能快地按下某些坐标。如果这里有人知道我的世界,我有一个箱子的截图,并且正在尝试实施,你必须点击箱子里的好物品。这就是为什么我需要访问胸部插槽坐标的原因。这是代码:

import tkinter as tk
from PIL import ImageTk, Image

window = tk.Tk()
window.title("Chest Simulator")
window.geometry("1920x1080")
window.configure(background='grey')

path = "chest.jpg"

Chest1

img = ImageTk.PhotoImage(Image.open(path))

panel = tk.Label(window, image = img)

panel.pack(side = "bottom", fill = "both", expand = "yes")

window.mainloop()

如果有人可以在这里帮助我,谢谢

标签: pythonvariablestkintercoordinates

解决方案


您可以使用 tkinter.bind()函数,然后在该函数内部检查单击的坐标是否在您的项目范围内。
对于物品类别,您必须输入物品命中框的左下角和右上角。单击后,hititems 列表将填充单击的项目。不要忘记在每次运行后清除它。

class item:
    def __init__(self,startx,starty,endx,endy):
        self.startx=startx
        self.starty=starty
        self.endx=endx
        self.endy=endy
items=[item(),item()...] #add your coordinates inside here
hititems=[]
def click(event):
    global items
    global hititems
    for i in items:
        if i.endy>=event.y>=i.starty and i.endx>=event.x>=i.startx:
            hititems.append(i)
panel.bind("<button1>",click)

推荐阅读