1

I'm working on an app to display a grid of images, much like the main screen of iPhoto or other similar programs. To do so, I set up a Canvas, and iterate through a list of filenames, creating a PhotoImage from each, and displaying them on the canvas:

self.canvas = Canvas(self.bottomFrame, width = 700, height = 470, bg = "Red")
self.canvas.pack()

for i, filename in enumerate(image_list):
    photo_image = PhotoImage(filename)
    self.canvas.create_image(100*(round(i/4)+1), 100*(i+1), image = photo_image)
    self.labelList.append(photo_image)

The labelList is an attribute of the Application class, and the image_list is populated with filenames of .gif photos. When I run the app, however, no images display. I know the canvas is there, because a red rectangle shows up, but there are no images on it.

What am I missing here - I've scrolled through endless pages of discussion looking for results and haven't found any that work.

3
  • What type of file is filename? Commented Sep 11, 2012 at 13:43
  • "The application must keep a reference to the image object." from effbot.org/tkinterbook/…. Sure you did? Commented Sep 11, 2012 at 13:45
  • @pythonm -- the reference should be kept in self.labelList. No problem there. Commented Sep 11, 2012 at 13:51

1 Answer 1

2
photo_image = PhotoImage(filename)

should be

photo_image = PhotoImage(file=filename)

Otherwise, you just set name, since the __init__ function of PhotoImage looks like this:

__init__(self, name=None, cnf={}, master=None, **kw)

Also note that PhotoImage can only handle GIF and PGM/PPM files. If you want other file types, you have to use PIL (example).

Sign up to request clarification or add additional context in comments.

3 Comments

argv! That's a seemingly silly way for them to define the function. (+1)
@mgilson Yeah, it is not intuitive at all. The main problem is that you don't get any feedback at all if you fall into this trap other than that no image is shown...
@BigYellowCactus Thank you! Worked perfectly. Now I'm just irritated that it was that simple...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.