23

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.

EasilyBaffled
  • 3,822
  • 10
  • 50
  • 87
  • If you mean `pygame`, [it works with 3.x](http://www.pygame.org/wiki/FrequentlyAskedQuestions#Does%20Pygame%20work%20with%20Python%203?), and it works on Mac. (It doesn't come with a binary installer, but really, I don't think you want the binary installers anyway. IIRC, they don't give you installers for 64-bit Python, or Apple's Python, or even the most recent python.org 32-bit Python.) – abarnert Apr 08 '13 at 18:44
  • As far as I know, the other common alternatives for 3D graphics are [`PyOpenGL`](http://pyopengl.sourceforge.net) and [`pyglet`](http://www.pyglet.org). Based on my somewhat limited and outdated experience, the former is probably better if you like thinking in terms of GL; the latter if you want to think in terms of windows and widgets and just render 3D surfaces to them. – abarnert Apr 08 '13 at 18:52
  • I heard about PyOpenGL, how easy is it to pick up, again I'm looking for as simple as possible, like playing with legos. – EasilyBaffled Apr 08 '13 at 18:59
  • If you already know GL, it's very easy to pick up. If you don't… probably not so much. The package does come with Python-translated versions of the [NeHe OpenGL tutorials](http://nehe.gamedev.net/), which are pretty good. But there's definitely an initial hurdle to getting into OpenGL, no matter how you do it. – abarnert Apr 08 '13 at 19:04
  • a hurdle like 4 hours in a night and I could build a rubix cube or two weeks and I just have a cube slide one position maybe on command – EasilyBaffled Apr 08 '13 at 19:23
  • Well… it took me a weekend to get the basics of GL down, but that was in C, on a limited-memory and very slow machine, and long before the NeHe tutorials existed. In [the 3.3 tutorial](http://www.opengl-tutorial.org) they get you to drawing a colored cube in the 4th tutorial, and more than half the time up to that point is dealing with build issues that won't affect you in Python, so… maybe give it a try and see how far you get in a couple hours? – abarnert Apr 08 '13 at 19:43

3 Answers3

28

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
John Henckel
  • 10,274
  • 3
  • 79
  • 79
Rushy Panchal
  • 16,979
  • 16
  • 61
  • 94
  • I just downloaded graphics.py, put it in the same directory as my main python file, added the code supplied above, and it works. No problem. – SMBiggs Nov 29 '19 at 19:06
  • 2
    @EasilyBaffled --- YES YOU CAN USE PIP --- just type `pip install graphics.py`. I just did it and I got version 5.0.1-post1. It works great on my Mac, without require gcc – John Henckel Apr 27 '20 at 00:20
  • Dirt simple is great. Especially when just teaching basic programming to kids. – AminM Apr 20 '22 at 04:51
6

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.

abarnert
  • 354,177
  • 51
  • 601
  • 671
4

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.

Hugh Fisher
  • 2,321
  • 13
  • 8