dynamic plots (Tkinter+PIL?)

Arnd Baecker arnd.baecker at web.de
Fri May 30 04:35:14 EDT 2003


Hi,

I would like to plot several thousands of points,
one after another, to create a "dynamic" appearance.

For this I have the following question concerning Tkinter+PIL:
I tried to use  ImageTk.PhotoImage However, it seems that
    ImageTk.PhotoImage.paste(self, im, box=None)
does not take care of the optional box argument
(looking at ImageTk.py from Imaging-1.1.4
seems to confirm this as there is no reference,
is this really true ?)

Because of this I use the approach as shown in the example below,
which updates the _whole_ picture.
Of course, this is pretty slow
(actually, would paste do the same and thus be as slow as well ?).

I am sure there must be a better/more efficient way to do
plot points in some canvas
(if possible I would like to avoid doing the tiling stuff
as in the example painter.py from PIL).

I also would not mind to hear about other alternatives
  (e.g. if there are any other add-ons to Tkinter which
  might do the job - eg. Pmw.Blt seems not suitable as
  it also stores the objects (If I am not mistaken).
  or other toolkits (as long as they are freely available
  both for Linux and Windows))

Many thanks in advance,

Arnd

####################################


"""
Example to demonstrate dynamic plots
(as a test just random numbers for (x,y) coords)

"""
from Tkinter import *

import Image, ImageTk
import random

class Test(Frame):
    def __init__(self, master=None):
	Frame.__init__(self, master)
	Pack.config(self)
	self.createWidgets()
	self.after(1, self.action)

    def createWidgets(self):
	self.QUIT = Button(self, text='QUIT', foreground='red',
			   command=self.quit)
	self.QUIT.pack(side=BOTTOM, fill=BOTH)

        # Frame in which everything else is placed
        self.Canvas_Nx=400
        self.Canvas_Ny=300
	self.draw = Canvas(self, width=self.Canvas_Nx,
height=self.Canvas_Ny)

        # Canvas in which we plot something ...
        self.Nx,self.Ny=200,150
        self.xoff,self.yoff=50,100

        # just draw a rectangle around the plot area:
        self.draw.create_rectangle(self.xoff,self.yoff,
                                   self.xoff+self.Nx,self.yoff+self.Ny)
        # Image for plotting
        self.im = Image.new("RGB",[self.Nx,self.Ny])
        # convert to TK usable one ...
        self.image = ImageTk.PhotoImage(self.im)
        # and create the image

self.draw.create_image(self.xoff,self.yoff,image=self.image,anchor=NW)
        # display:
 	self.draw.pack(side=LEFT)


    def action_(self):
        #print "Action -  modify picture !!!"
        xr=random.randint(0,self.Nx-1)  # random coords for test
        yr=random.randint(0,self.Ny-1)
        col=random.randint(0,255)       # random color...
        self.im.putpixel((xr,yr),(255,0,col))

        self.image.paste(self.im)   # paste into the PhotoImage
        # plot several of these points ...
        self.after(1, self.action)

    def action(self):
        """ this one does not pay attention to the place, (xr,yr)"""

        #print "Action -  modify picture !!!"
        xr=random.randint(0,self.Nx-1)  # random coords for test
        yr=random.randint(0,self.Ny-1)
        col=random.randint(0,255)       # random color...

        # create a small image to be pasted
        im=Image.new("RGB",[10,10],(col,255-col,col))
        self.image.paste(im,box=(xr,yr))   # paste into the PhotoImage

        # plot several of these points ...
        self.after(1, self.action)


test = Test()
test.mainloop()






More information about the Python-list mailing list