Pygame 从位图设置鼠标光标
我正在使用 pygame 制作图像编辑器,我想知道是否可以将鼠标光标更改为更适合画笔的内容(例如圆形或矩形)。 Pygame 有一种非常奇怪的方法,我不确定它是否能很好地工作。有没有办法可以写入位图然后使用它?
如果有一种方法可以用 Python 来实现,我想那也是可行的。
I'm making an image editor using pygame and I was wondering if it was possible to change the mouse cursor to something more suitable (for instance a circle or rectangle) for brushes.
Pygame has a really weird way of doing it that I'm not sure would work very well. Is there a way I can write to a bitmap and then use that?
If there is a way to do it with Python generally, that would work too I suppose.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
另一种选择是简单地隐藏光标,加载您喜欢的任意位图和 绘制每一帧 光标所在位置。
Another option is to simply hide the cursor, load any arbitrary bitmap that you like and draw it every frame where the cursor is.
您可以使用 pygame.cursors.load_xbm——当然,这只是黑白光标,这是 PyGame 的一个有据可查的限制,根据 它的文档(我引用):
有关 XBM 格式文档,请参阅此处。您可以根据其文档。
You can load cursors in PyGame with pygame.cursors.load_xbm -- that's black and white cursors only, of course, a well-documented limitation of PyGame, per its docs (and I quote):
For XBM format docs, see e.g. here. You can prep such files e.g. with the PIL library, per its docs.
由于@SapphireSun 在评论中询问了另一种方式,我想提出一个不同的答案,这是手动绘制光标。我想这就是@Mizipzor 在他的回答中所建议的,所以也许这只是一个阐述。
首先隐藏鼠标光标,然后每次更新屏幕框架时,“手动”绘制光标:
此方法允许 PyGame 程序具有任何类型的光标位图。在正确的位置获取“单击”热点可能会有些棘手,但这可以通过设置透明光标来实现,并使热点位于与自定义位图匹配的位置。有关详细信息,请参阅手册。
我不确定我对多色光标的感觉如何,但至少这是可能的。
Since @SapphireSun asked in a comment about another way, I'd like to present a different answer, and this is drawing the cursor manually. I guess this is what @Mizipzor suggests in his answer, so maybe this is just an elaboration.
First Hide the mouse-cursor, then every time the screen-frame is updated, "manually" paint the cursor:
This method allows the PyGame program to have any sort of bitmap for the cursor. There may be some trickiness around getting the "click" hot-spot in the right location, but this could be achieved by setting a transparent cursor, with the hot-spot at the position to match the custom bitmap. See the manual for details.
I am not sure how I feel about multi-coloured cursors, but at least it's possible.
您也可以简单地加载图像来替换或绘制一些东西来代替它。
举个例子:
如果你只想用一个形状替换它,你可以这样做:
希望这有帮助!
you could also just simply load a image to replace of draw something instead of it.
For an example:
If you just want to replace it with a shape you could just do this:
Hope this helped!