如何解决如何在鼠标按下时在 Pyglet 中的图像上绘制形状?
我想在 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,batch=gui.batch)
circles.append(circle)
pyglet.app.run()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。