Luis
Luis

Reputation: 43

Tkinter does not show a picture on canvas when the canvas is put inside a frame

This piece of code works fine:

import Tkinter
from PIL import ImageTk, Image

root = Tkinter.Tk()

Image_ = Image.open("optical.jpg")
ImageForTK = ImageTk.PhotoImage(Image_)

ImageCanvas = Tkinter.Canvas(root, width = Image_.size[0], height = Image_.size[1])           
ImageCanvas.pack()
ImageCanvas.create_image(0, 0, anchor = Tkinter.NW, image = ImageForTK)
root.mainloop()

In this second piece of code the image is not shown, because I've added a frame around the canvas object showing the picture.

import Tkinter
from PIL import ImageTk, Image

root = Tkinter.Tk()

Image_ = Image.open("optical.jpg")
ImageForTK = ImageTk.PhotoImage(Image_)

Frame = Tkinter.Frame(root)
# EDIT:
Frame.pack()
# END EDIT
ImageCanvas = Tkinter.Canvas(Frame, width = Image_.size[0], height = Image_.size[1])           
ImageCanvas.pack()
ImageCanvas.create_image(0, 0, anchor = Tkinter.NW, image = ImageForTK)
root.mainloop()

Does anybody have an idea what is goint wrong here?

Thanks for the answer. Missed this one.

Upvotes: 4

Views: 1913

Answers (1)

unutbu
unutbu

Reputation: 879561

The frame itself needs to be packed:

Frame = Tkinter.Frame(root)
Frame.pack()

For a widget to be seen, it and all of its parent widgets must be registered with a geometry manager, which can be done with the pack, grid or place methods.

Upvotes: 2

Related Questions