Direct pixel-by-pixel drawing (with Tk)

Fredrik Lundh fredrik at effbot.org
Wed Dec 13 18:18:53 EST 2000


Jonathan Lange wrote:
> I'm relatively new to Python (but not to programming), and I'd like to
> muck around by doing some fractal stuff. However, alot of this stuff
> needs (wants?) setting pixels directly.

Create a PhotoImage object, fill it with data, and display it.

One way to update a PhotoImage object is to use the "put"
method.  This method takes a tuple of pixel rows, where each
row is a tuple containing color values (names or #rrggbb hex
strings).

    label = Label()
    label.pack()

    data = (
        ("#000000", "#404040"),
        ("#808080", "#FFFFFF")
    )

    im = PhotoImage(width=len(data[0]), height=len(data))
    im.put(data)

    label.config(photo=im)
    label.im = im # make sure to keep a reference!

Another approach is to use PIL's ImageTk module, and do the
drawing in a PIL Image memory.

PIL sources and docs are available from PythonWare:

    http://www.pythonware.com/products/pil/index.htm

</F>

<!-- (the eff-bot guide to) the standard python library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->





More information about the Python-list mailing list