如何在 Python 中绘制网格和矩形?
您可以轻松地使用哪些 Python 相关代码(PyGTK、Glade、Tkinter、PyQT、wxPython、Cairo 等)来创建 GUI 来执行以下部分或全部操作?
- GUI 的一部分有一个不可移动的方形网格。
- 用户可以按下按钮来创建可调整大小的矩形。
- 用户可以将矩形拖动到网格上的任何位置,它将捕捉到网格。
What Python-related code (PyGTK, Glade, Tkinter, PyQT, wxPython, Cairo, ...) could you easily use to create a GUI to do some or all of the following?
- Part of the GUI has an immovable square grid.
- The user can press a button to create a resizable rectangle.
- The user can drag the rectangle anywhere on the grid, and it will snap to the grid.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
PyQt 附带的DiagramScene Eaxmple 实现了您想要的大部分功能。它有一个固定的背景网格,您可以创建一个矩形对象,但它不能调整大小,也不能捕捉到网格。
这篇SO文章提供了有关使用鼠标调整图形对象大小的建议。它适用于 C++ Qt,但该技术应该很容易在 PyQt 中复制。
对于对齐网格,我认为没有任何内置功能。您可能需要重新实现 itemChange(GraphicsItemChange change, const QVariant &value) 函数。伪代码:
重新定位该项目将导致 itemChange 再次被调用,但这没关系,因为该项目将被正确定位并且不会再次移动,因此您不会陷入无限循环。
The DiagramScene Eaxmple that comes with PyQt implements much of the functionality you want. It has a fixed background grid, you can create a rectangle object but it's not resizable and doesn't snap to grid.
This SO article has advice on resizing graphical objects with the mouse. It's for C++ Qt but the technique should be easy to replicate in PyQt.
For snap-to-grid I don't think there is any built-in functionality. You would probably need to reimplement the itemChange(GraphicsItemChange change, const QVariant &value) function. Pseudocode:
Repossitioning the item will cause itemChange to get called again, but that's ok because the item will be possitioned correctly and won't be moved again, so you'll not be stuck in an endless loop.
我一直在寻找这样的东西一段时间,最后设法用 Python
wx
编写了一个“最小”的工作示例,利用wx.lib.ogl
及其 < code>Diagram 和ShapeCanvas
类。代码(如下)的结果如下:注意:
ogl
是否允许将该锚点更改为,例如,左下角)该示例使用
MyPanel
类进行自己的绘图,并且继承自ogl.ShapeCanvas
和wx.Panel
code> (尽管可以删除带有wx.Panel
的 mixin,并且代码仍然会以相同的方式工作) - 然后将其添加到wx.Frame
中。请注意一些警告的代码注释(例如使用ogl.ShapeCanvas
阻止所有按键事件,除非首先对该小部件执行SetFocus
)。代码:
I was looking for a while for something like this, and finally managed to cook up a "minimal" working example with Python
wx
, utilizingwx.lib.ogl
and itsDiagram
andShapeCanvas
classes. The code (below) results with something like this:Note:
ogl
ogl
ogl
allows for changing that anchor to, say, bottom left corner)The example uses a
MyPanel
class that does its own drawing, and inherits both fromogl.ShapeCanvas
and fromwx.Panel
(though the mixin withwx.Panel
can be dropped, and the code will still work the same) - which is then added to awx.Frame
. Note the code comments for some caveats (such as the use ofogl.ShapeCanvas
blocking all key events, unless aSetFocus
is performed on that widget first).The code:
这些动作并不那么困难。您真正需要的是命中检测,这并不难(光标是否位于正确的区域?好的,然后执行操作)。更困难的部分是为正在使用的工具包找到合适的画布小部件。
Those actions are not that difficult. All you really need for that is hit detection, which is not hard (is the cursor over the correct area? Okay, perform the operation then). The harder part is finding an appropriate canvas widget for the toolkit in use.