views:

64

answers:

2

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?

  1. Part of the GUI has an immovable square grid.
  2. The user can press a button to create a resizable rectangle.
  3. The user can drag the rectangle anywhere on the grid, and it will snap to the grid.
A: 

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.

Ignacio Vazquez-Abrams
@Ignacio Vazquez-Abrams: For Tkinter what you say is the hard part is pretty easy: use the Canvas widget.
Bryan Oakley
A: 

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:

if (object not possitioned exactly on the grid):
    (possition the item on the grid)

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.

Simon Hibbs
The DiagramScene example repeats an image to create a grid. It may or may not be difficult to snap objects to such a grid.
Winston C. Yang
That's true, perhaps better to create a grid of squares. I do that in one of my apps using a QGraphicsItemGroup with each grid cell belonging to the group.
Simon Hibbs