-4

In Python 2.7, I want to display some simple graphics, such as a red square moving around on a blue window. On every frame, I want to update the position of the square, and render the new window. I am looking for something simple and lightweight.

I have seen people use matplotlib for drawing shapes, but I don't want to have to deal with axes and data points. I have also seen pygame suggested, but this seems to heavyweight for what I want, as I do not want to create a game, just a simple animation.

So what I really want is something where on every frame, I just indicate the colour of every pixel on an image, and then display that image. What are some good suggestions?

Bryan Oakley
  • 310,202
  • 36
  • 445
  • 584
Karnivaurus
  • 18,315
  • 44
  • 129
  • 209
  • There are many questions and answers related to animation on this site. You should do a bit more research before asking a question, try one of the solutions in one of the other answers, and then come back to ask a specific question when you get stuck. – Bryan Oakley Mar 08 '16 at 16:07
  • For a complete working example see http://stackoverflow.com/questions/6740855/board-drawing-code-to-move-an-oval/6789351#6789351 – Bryan Oakley Mar 08 '16 at 16:12

1 Answers1

2

Tkinter is not good for setting individual pixels. If you want to move rectangles or ovals though (a small oval will look like a pixel, but it doesn't scale for updating a whole image).

def update(x,y):
    canvas.delete('all')
    canvas.create_rectangle(x-1,y-1,x+1,y+1)

You can, of course, be more judicious, saving the return value of the rectangle and then only clear the appropriate elements. Or you can move existing elements directly as Bryan points out. As he explains elsewhere Tkinter of course, supports drawing images, ovals, and a slew of other things. Here's a canonical source edit: that is old and not canoncial This one's slightly better For a general source on animating with a timer loop, here's Bryan agian

Bryan also noted that you can work with pixels directly You can do that with PhotoImage.

Array-Like Pixel Access Without Graphical Extensions

A robust module like pygame will be the most scalable option. However, I've had success (in educational settings only) writing graphics engines by modifying the elements of a numpy array and then displaying it as an image (you also need this link to display the images).

This lets you do pixel level modifications; since it's relatively trivial to write C-extensions that modify numpy arrays, you can prototype fast image processing doing custom manipulations. While I've written whole graphics engines using just tkinter this way, again I can only reccomend it for educational purposes.

Otherwise, just bite the bullet and pull in openGl or pygame. You'll save yourself a ton of time in the long run.

Summary

  • Very simple animations can be done right in tkinter
  • For educational purposes, you can do arbitrary graphics with numpy and tkinter
  • For rhobust animations, check out a full library (openGl, matplotlib, pygame) that suits your needs (graphical rendering, statistical graphing, game development, etc.)
Community
  • 1
  • 1
en_Knight
  • 4,793
  • 1
  • 23
  • 40
  • 1
    there's no need to delete and recreate items on a canvas. You can move existing items. Also, you refer to a canonical example of doing animation, but that example is definitely not canonical. Finally, it's quite possible to manipulate individual pixels in an image using tkinter. It's a bit slow, but it's fast enough if you're not changing too many individual pixels at once. – Bryan Oakley Mar 08 '16 at 16:10
  • @BryanOakley better? I feel like that source is canonical enough for time travellers from 1998 :) (didn't look at the date when I grabbed the effbot page). Is the PIL method what you meant by pixel manipulation? I've seen you write tcl extensions to modify widgets, but I'm not familiar with any tkinter-native direct pixel access routines – en_Knight Mar 08 '16 at 16:48
  • You don't need PIL to manipulate pixels. The [PhotoImage](http://tkinter.unpythonic.net/wiki/PhotoImage) class (part of tkinter) has methods to get and set pixels. – Bryan Oakley Mar 08 '16 at 16:51
  • @BryanOakley oh... ya that's what the link I posted uses. I thought PhotoImage was in PIL... oops. I'll make that edit too – en_Knight Mar 08 '16 at 16:55