What happened to Python's rect class? - python

On a Google Search, I found this article:
http://docs.python.org/release/1.4/lib/node201.html
Which showed examples of using the rect class, to perform union/intersections/checking if points are inside rect. Importing rect fails in Python 2.7. Is this class in another package?

I assume the question isn't really "what happened to it?", but "where can I find a class like this that I can use?".
Most GUI libraries have a class like this. For example: wx.Rect, QRect/QRectF, gtk.gdk.Rectangle, PyGame rect.
If you want a generic rectangle class without the overhead of a GUI library, I think your only option is Sympy, which has a Geometry module that may suit your needs.
Finally, you may just choose to roll your own. It's not very hard to make such a class.

It's part of the Standard Windowing interface. To quote from the documentation you yourself linked to:
Using STDWIN is not recommended for
new applications. It has never been
ported to Microsoft Windows or Windows
NT, and for X11 or the Macintosh it
lacks important functionality -- in
particular, it has no tools for the
construction of dialogs.

The stdwin module containing rect class was obsolete since version 1.6 (see these notes) and I believe it was removed in some later version.

I think the rest of the answers have covered you. However I was in your shoes, and made a generic module to benefit more people. You can find the project here. The module works with both negative and positive numbers on a screen coordinate system (y grows downwards of screen, x grows to the right).
Some methods supported:
Distance from one rectangle to an other
See if rectangles overlap on x, y or both axis (default)
See if a point is inside a rectangle
Align rectangles

Related

How to drag objects around in a Gtk.Fixed widget using mouse events?

I have a custom object(a small circle) placed at some point inside a Gtk.Fixed() widget .Is there a way to drag this object around using the mouse. I am not able to map the Mouse Press/Release/Motion events to make this work.
I would prefer solution in Python using PyGobject but any other language will do also be fine if explanation is provided
More Details:
I am trying to make a font editor where these objects I mentioned above will be the control points of the bezier curves in the Glyph outlines
Here is an image of the concept design:
https://github.com/sugarlabs/edit-fonts-activity/blob/gh-pages/files/img/wireframe_concept_01_first_prototype.svg
I need to able to move the points shown to edit the outline of the letter shown
GtkFixed is not designed to do drawing work. It's made to locate widgets (such as buttons and such) on a fixed grid (á la Windows).
If you would like to move elements of a drawing, have a look at eg. GooCanvas. Each element on a goocanvas can have events connected, which can then be used to move it around. You can even use CanvasGroup to group primitives (circle, rectangle etc), and move them together (even change other properties such as color, linewidth). The toolbox actually contains curves etc. It's easy to create a 'handle' using a small rectangle.
Here's an example of a simple goocanvas program, and you can find download links, references manuals and other useful stuff here.
I don't know if this is a tool you need, or just a learning exercise. If the former, then do have a look at FontForge, an open source font editor, and incredibly versatile.

Pygame draw behavior

I am currently designing basic pygame projects. Random projects actually, which are just random ideas pop into my head during common daytime. One of them is a wall clock. Where I use sin, cos functions to draw hour hand, minute hand etc..
As a result of these functions, I obtain float values. Which pygame does not allow. So I round and int() all values before using them in drawing functions. This makes sense because of pixels can't be partially filled. However, I think this results in bad drawings.
The Image above is the clock drawn by the pygame module. As you see the handles are somewhat leaning. Not straight.
I also implemented this in codeskulptor. Which is a python2 environment for basic game(or any graphical) programming. Has nice and clear functions, very easy to use indeed. The float values I use in that no needs conversion and accepted directly. I do not know how it handles it, but it is clearly better. Obviously not just rounding and integering(does this word exist ?) values. Let me show you the clock drawn in codeskuptor:
As you can see, the edges are more smooth. The lines does not end sloped, but straight. It is clearly a better drawing than the pygame one.
But the thing is, codeskuptor does not implement many modules and built-in functions that is harder than beginning level knowledge. Also it doesn't support a well-known compiler(basically not a python module). So can't work on computer and can not be compiled with py2exe, pyinstaller as such.
So I wan't to implement all this in pygame, and get smooth results just like in codeskulptor. Maybe a better way to handle float points for drawing. Any idea or knowledge in this area would be greatly appreciated.
My code for codeskuptor(Does not show realtime)
My code for pygame
The codes does not supply proper commenting because they were just a late-night fun nothing more. I'll explain any necessary parts.
To summarize the comments above, the issue you've noticed here is antialiasing. For the lines, pygame provides the aaline function, which can be used to draw a single line. Unfortunately, this does not support a varying thickness. Potential workarounds could be drawing a line of the correct thickness and drawing an aaline on either side of it, or drawing the line using an aapolygon (from the gfxdraw module).
The standard draw module does not include antialiased circles, but you can use another function from gfxdraw to draw an antialiased circle, aacircle.
It is important to note that the gfxdraw module is labeled "experimental", so the functions are not guaranteed to persist across versions of pygame. But for your quick projects that might not be a concern.
As for CodeSkulptor, they are rendering the lines on an HTML5 <canvas> element, which according to this question has anti-aliasing turned on by default.

simple graphics for python

I am making a simulation in python that needs visualization. I am looking for the most simple graphics library python has. Is there anything that can let me do something along the lines of:
setWindow(10,10)
setCube(0,0,1,1,1)#windowX, windowY, width, length, hight,
updateList = simUpdate
for update in updateList:
removeShape(0,0)
setCube(0,0,1,1,1)
Is there anything that simple? 3d is not a must but it would be nice.
I'm working in python 3.3 and pygames hasn't been updated for that on a mac as far as I can tell. I've been working with tkinter but would like something a little easier.
For simple graphics, you can use graphics.py.
It's not included with Python, so you should save it as a Python file (preferably named graphics.py) where Python can see it --- on your sys.path.
Note: it is also available using pip install graphics.py see link
It's very easy to learn and has various shapes already built-in. There are no native 3D graphics (none are in there) but it's easy to do so: for a cube, draw one square and another one to the side, and connect one corner of a square to the corresponding corner in the other square.
Example using graphics.py to create a square:
from graphics import *
win = GraphWin(width = 200, height = 200) # create a window
win.setCoords(0, 0, 10, 10) # set the coordinates of the window; bottom left is (0, 0) and top right is (10, 10)
mySquare = Rectangle(Point(1, 1), Point(9, 9)) # create a rectangle from (1, 1) to (9, 9)
mySquare.draw(win) # draw it to the window
win.getMouse() # pause before closing
pygame does work with 3.x, and with Mac OS X. There are no binary installers for any recent Mac version, but that's not much of a loss, because the binary installers were never very good. You will basically have to follow the Unix install directions, and figure out for yourself how to install all the prereqs (if you use Homebrew it's just brew install sdl sdl_ttf … jpeg libpng), then figure out how to tell the setup how to find all those prereqs. You may want to look at (or maybe even use) this recipe, adapted to your own particular Python installation.
As far as I know, the other major alternatives for 3D graphics are PyOpenGL and pyglet. The former is a pretty direct wrapper around OpenGL, which is great if you think in GL terms. The latter is a higher-level library, somewhat similar to pygame but built directly on native OpenGL and native windows on each platform, instead of on SDL.
You also might want to look at Python 3D Software Collection, which is maintained by the main author of PyOpenGL. I have no idea how up-to-date it is, but he's still linking to it from the PyOpenGL site and docs.
If you're looking for something mathematically-oriented, you may want to look at matplotlib, mplot3d and friends. If you're ultimately trying to graph complex geometric shapes and transform/intersect/etc. them, this is your friend. If you're trying to build a simple game or quasi-CAD program, it's not.
Visual Python, from www.vpython.org
Designed for people who aren't OpenGL hackers. It has a retained scene graph model rather than immediate, some built in controls, and there are quite a lot of scientific / engineering simulation examples floating around the Internet.

Basic Python 2D graphics?

I'm looking for a Python 2D graphics library that can basically do the following and not necessarily anything more:
Create a window of specified width and height
Set the RGB of pixel X, Y on the back buffer.
Swap buffers
...and that's it basically. I can't find anything that doesn't come with a massive amount of complex baggage.
I recommend PyQt for this - it's a GUI library/framework but it has very good drawing capabilities. If you look at the examples coming with PyQt, focusing on the graphics & drawing samples, it's quite amazing what you can do with very few lines of code.
Oh, and it does the double-buffering you mention automatically so you don't have to worry about it.
Alternatively, you can use PyGame - a library wrapping SDL, used for game development. Naturally it has very strong 2D graphics capabilities.

Python Canvas library for geometric shapes [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
I'm looking for a Python library for creating canvases for manipulating geometric shapes. Specifically I need the ability to create arbitrary polygons and place them on the canvas, the polygons need to have the ability to be transparent/have an alpha channel, I need to be able to edit polygons that are currently on the canvas, and I need to be able to get the actual color of a given pixel(the aggregate of all the transparent piece that are there).
Basically I'm trying to make this: http://alteredqualia.com/visualization/evolve/ in python.
I think cairo will do a lot of what you want. They have python bindings, too.
The one requirement that that won't help you with is modifying previously-drawn polygons, but I don't know of any canvas that will do that for you.
Sounds like a job for OpenGL.
My advice is that, whichever library you choose, you make a data structure for your polygons that suits your algorithms so that they can be more simple and readable rather then try to get these algorithms to manipulate a canvas directly. Then you can write the code that draws them separate (i.e. independent) of the main logic.
This discussion on Stackoverflow has some comparisons and code snippets on various GUI toolkits for Python. I'm pretty sure that the QGraphicsView on QT will do transparency. Nokia (nee Troll) make a demo suite for QT that should give you an idea of its capabilities.
Try pyglet. It is a graphics library for Python with OpenGL. If you've done OpenGL programming before, it is certainly the easiest way to get what you want.
I believe the HTML canvas lets you modify elements
It does not. You can check out my HTML canvas tutorial to see how you draw a moving ball; you wipe the screen and draw a new circle at the spot you want.
You can draw simple shapes to a canvas in all of pyglet, pygame, QT, Tkinter, wxPython and cairo.
Generally, you will have objects called "sprites" or "shapes" that represent objects drawn to the screen, and you'll store them all in a container. Then the library or framework will, at every frame, render them all to the canvas. Thus it will seem to the user (you) that you can modify the objects on screen; you set a ball's x and y coordinates and in the next frame it's rendered there. However, at a low level, everything's being wiped and redrawn again.
For computationally intensive animation, a technique called double-buffering will be employed whereby a bitmap in memory will be modified instead of the one onscreen, and then the drawing process will simply be to copy that bitmap to the screen.
alter the item in the list and then create a new canvas, which seems like it would have a significant overhead.
All of the frameworks mentioned above will give you a nice abstraction for the list of objects to draw, so that you won't need to maintain it manually, and you can program as if the sprites/shapes you've drawn can be directly moved onscreen, even though they really aren't at a low level.
Pygame should be able to do this for you.
See pygame.draw.polygon
I believe the HTML canvas lets you modify elements, which makes me believe there might be another canvas that can as well. However, if there is not that would basically require me to keep a separate list of all the polygons and when I wanted to make a change, alter the item in the list and then create a new canvas, which seems like it would have a significant overhead.
Both Qt and wxWidgets have some canvas drawing abilities (Qt calls it GraphicsView). Quick Google searches will get you a lot of examples so you can see if it fits your requirements.

Categories