按下鼠标后如何在 Pyglet 中的图像上绘制形状?

How to draw a shape on an image in Pyglet upon mouse press?

我想在 Pyglet 中显示图像。然后在单击鼠标时,我想绘制一个形状(比如说一个圆圈)来标记图像上的单击点。这是我目前所拥有的

window = pyglet.window.Window(width=1600, height=700)
gui = glooey.Gui(window)
hbox = glooey.HBox()

# Loading the image
image = pyglet.image.load("image.png")
img_widget = glooey.images.Image(image=image, responsive=True)
hbox.add(img_widget)

hbox.add(glooey.PlaceHolder())
gui.add(hbox)

# Mouse press events
@img_widget.event
def on_mouse_press(x, y, buttons, modifiers):
    # If left shift + left mouse button is pressed
    if modifiers == 1 and buttons == 1:
        circle = shapes.Circle(x=x, y=y, radius=10, color=(50, 225, 30))
        circle.draw()
        window.flip()
    return

pyglet.app.run()

当我点击时,window.flip() 行使圆圈闪烁。如果我删除它,我什么也看不到。如何使这些形状持久存在?我的目标是点击图像中的几个点并同时显示它们。

感谢您的帮助!

一个存储圈子的列表:

circles = []

创建一个圆形并设置 batch 参数 (batch = batch=gui.batch)。将形状附加到列表中:

circle = shapes.Circle(x=x, y=y, radius=10, color=(50, 225, 30), batch=gui.batch)
circles.append(circle)

完整示例

import pyglet
from pyglet import shapes
import glooey

window = pyglet.window.Window(width=1600, height=700)
gui = glooey.Gui(window)
hbox = glooey.HBox()

# Loading the image
image = pyglet.image.load("image.png")
img_widget = glooey.images.Image(image=image, responsive=True)
hbox.add(img_widget)

hbox.add(glooey.PlaceHolder())
gui.add(hbox)

circles = []

# Mouse press events
@img_widget.event
def on_mouse_press(x, y, buttons, modifiers):
    # If left shift + left mouse button is pressed
    if modifiers == 1 and buttons == 1:
        circle = shapes.Circle(x=x, y=y, radius=10, color=(50, 225, 30), batch=gui.batch)
        circles.append(circle)

pyglet.app.run()