1

I'm unable to find any way to load .bmp file into Tkinter() so that I can use it in a canvas widget!Plz help me!

from Tkinter import *
from PIL import Image
import ImageTk
import tkFileDialog
import tkMessageBox
root=Tk()
class lapp:
   def __init__(self,master):
      w=Canvas(root,width=300,height=300)
      w.pack()
      p=Image.open("001.bmp")
      tkimage=ImageTk.PhotoImage(p)
      w.creat_image(0,0,image=tkimage)
App=lapp(root)
root.mainloop()

Its not showing any image on the canvas, its just blank! Btw I'm using win7 with python 2.7

luc
  • 41,928
  • 25
  • 127
  • 172
Manik
  • 13
  • 1
  • 1
  • 4
  • Can you provide us with a little bit more of details? Possibly a stack trace or a source code snippet would give us some glues what you are actually doing. – jsalonen Oct 12 '10 at 09:16

1 Answers1

1

This works for me.

The image doesn't show when I use the Tk PhotoImage class. But it works ok when using PIL.

My image size is 50*250, so I've put coordinates that center it (25, 125)

from Tkinter import *
from PIL import Image, ImageTk

root=Tk()

root.title("My Image")

w = Canvas(root, width=50, height=250)
image = Image.open("blog0.bmp")
w.create_image((25, 125), image=ImageTk.PhotoImage(image))

w.pack()

root.mainloop()

I hope it helps

luc
  • 41,928
  • 25
  • 127
  • 172
  • Traceback (most recent call last): File "C:\Users\Administrator accoun\Desktop\Graphics\imageload", line 9, in image = Image.open("G:/pop.bmp") File "C:\Python27\lib\site-packages\PIL\Image.py", line 1980, in open raise IOError("cannot identify image file") IOError: cannot identify image file – Manik Oct 13 '10 at 16:37
  • Strange if PIL can not open your image. Have you tried with another bmp? – luc Oct 14 '10 at 04:42
  • Ho solved you just gotta make p and Tkimage global variables that's it! – Manik Oct 14 '10 at 10:30