problems with photoimage and tkinter

Seth Jones sjones16 at rochester.rr.com
Thu Feb 28 05:05:12 CET 2002


Hi, I'm looking for some help using images in tkinter. I've spent hours with
it, and I can't
seem to get anything working. Well, I have something. The following code is
from the Python Imaging Library 'painter.py'.. This is the minimum necessary
to display an image on the canvas.
The line

                #self.tile[(x,y)] = box, tile

must be uncommented in order for this code to work, and for the life of me,
I can't imagine why. I can't see how it has anything to do with the rest of
the code, but over and over, I've commented it and ran the code and
uncommented it and ran it. The results are consistent. (It was needed for
some code that I deleted.) The file being loaded is a 160 x 120 Windows
Bitmap. I'm using python 2.2 on windows 95. What i'd like is to be able to
just display a single color image on a canvas. I'd really appreciate some
help; I was in love with python until I spent the past few days being
bewildered by this. (^ :

Thanks to anyone who can enlighten me.

Keith Jones

----------------------------------------------
from Tkinter import *
import Image, ImageTk
import sys

class PaintCanvas(Canvas):
    def __init__(self, master, image):
        Canvas.__init__(self, master, width=image.size[0],
height=image.size[1])

        self.tile = {}
        self.tilesize = tilesize = 32
        xsize, ysize = image.size
        for x in range(0, xsize, tilesize):
            for y in range(0, ysize, tilesize):
                box = x, y, min(xsize, x+tilesize), min(ysize, y+tilesize)
                tile = ImageTk.PhotoImage(image.crop(box))
                self.create_image(x, y, image=tile, anchor=NW)
                #self.tile[(x,y)] = box, tile


root = Tk()

im = Image.open('d:\\cyberdeck\\firstattempt\\b3.bmp')

if im.mode != "RGB":
    im = im.convert("RGB")

PaintCanvas(root, im).pack()

root.mainloop()
sys.exit()
------------------------------------------





More information about the Python-list mailing list