如何将.bmp文件加载到BitmapImage类Tkinter python中 [英] How to load .bmp file into BitmapImage class Tkinter python

查看:285
本文介绍了如何将.bmp文件加载到BitmapImage类Tkinter python中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到任何方法将.bmp文件加载到Tkinter()中,因此无法在画布小部件中使用它!请帮我!

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()

它在画布上没有显示任何图像,只是空白! 顺便说一句,我在python 2.7中使用win7

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

推荐答案

这对我有用.

当我使用Tk PhotoImage类时,图像不显示.但是在使用PIL时可以正常工作.

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

我的图片尺寸为50 * 250,所以我已将坐标居中(25、125)

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()

我希望对您有帮助

这篇关于如何将.bmp文件加载到BitmapImage类Tkinter python中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆