【问题标题】:empty Tkinter window appears in place of image空的 Tkinter 窗口出现在图像的位置
【发布时间】:2014-03-15 12:42:05
【问题描述】:

我正在尝试通过浏览按钮将图像动态加载到 Tkinter 窗口,但我得到的是空窗口。这是浏览按钮的回调函数代码

supformats = [
('Windows Bitmap','*.bmp'),
('Portable Network Graphics','*.png'),
('JPEG ','*.jpg'),
('CompuServer GIF','*.gif'),
]
filename = askopenfilename(filetypes=supformats)
FILENAME = filename
im=Image.open(FILENAME)
w=im.size[0]
h=im.size[1]
root = Tkinter.Tk()
#canvas = Tkinter.Canvas(root, width=w, height=h)
#canvas.grid(row=0,column=0)
#tk_img = ImageTk.PhotoImage(file = FILENAME)
#canvas.create_image(image=tk_img)
im.show()
root.mainloop()

提前感谢所有愿意提供帮助的人

【问题讨论】:

  • 你可能想参考this

标签: python tkinter python-imaging-library


【解决方案1】:

来自 effbot 的解释 http://effbot.org/tkinterbook/photoimage.htm 请注意,该文件只被访问一次,而不是两次。这假设您在 Tkinter 中使用 Python Imaging Library,因为您没有说明正在使用什么图像工具包。

from PIL import Image, ImageTk

image = Image.open("lenna.jpg")
photo = ImageTk.PhotoImage(image)

You can use a PhotoImage instance everywhere Tkinter accepts an image object.
An example:
label = Label(image=photo)
label.image = photo # keep a reference!
label.pack()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-26
    • 1970-01-01
    • 2020-01-25
    相关资源
    最近更新 更多