simple graphics for python

92,733

Solution 1

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

Solution 2

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.

Solution 3

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.

Share:
92,733
EasilyBaffled
Author by

EasilyBaffled

Front end web developer. Working primarily with Javascript HTML and CSS, though Python will always hold a special place in my heart.

Updated on July 09, 2022

Comments

  • EasilyBaffled
    EasilyBaffled almost 2 years

    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.

  • SMBiggs
    SMBiggs over 4 years
    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.
  • John Henckel
    John Henckel about 4 years
    @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
  • AminM
    AminM about 2 years
    Dirt simple is great. Especially when just teaching basic programming to kids.