How to load .bmp file into BitmapImage class Tkinter python

不羁岁月 提交于 2019-12-11 08:33:06

问题


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


回答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



来源:https://stackoverflow.com/questions/3913037/how-to-load-bmp-file-into-bitmapimage-class-tkinter-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!