40

如何在 Tkinter 中添加图像?

这给了我一个语法错误:

root = tk.Tk()
img = ImageTk.PhotoImage(Image.open(path))
panel = tk.Label(root, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")
root.mainloop()
4

9 回答 9

37

2013 年 5 月 14 日 win32 上的 Python 3.3.1 [MSC v.1600 32 位(英特尔)]

按照上面的代码,这对我有用

from tkinter import *
from PIL import ImageTk, Image
import os

root = Tk()
img = ImageTk.PhotoImage(Image.open("True1.gif"))
panel = Label(root, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")
root.mainloop()
于 2013-05-14T06:29:21.057 回答
17

上面的代码中没有“语法错误”——它要么发生在其他行(上面不是你的所有代码,因为没有导入,也没有你的path变量的声明),或者你得到了一些其他错误类型。

上面的例子对我来说很好,在交互式解释器上进行测试。

于 2012-04-13T02:56:35.470 回答
10

这是 Python 3 的示例,您可以针对 Python 2 进行编辑;)

from tkinter import *
from PIL import ImageTk, Image
from tkinter import filedialog
import os

root = Tk()
root.geometry("550x300+300+150")
root.resizable(width=True, height=True)

def openfn():
    filename = filedialog.askopenfilename(title='open')
    return filename
def open_img():
    x = openfn()
    img = Image.open(x)
    img = img.resize((250, 250), Image.ANTIALIAS)
    img = ImageTk.PhotoImage(img)
    panel = Label(root, image=img)
    panel.image = img
    panel.pack()

btn = Button(root, text='open image', command=open_img).pack()

root.mainloop()

在此处输入图像描述

于 2017-04-20T02:17:31.530 回答
7

以下代码适用于我的机器

  1. 您的代码中可能缺少某些内容。
  2. 请同时检查代码文件的编码。
  3. 确保你安装了 PIL 包

    import Tkinter as tk
    from PIL import ImageTk, Image
    
    path = 'C:/xxxx/xxxx.jpg'
    
    root = tk.Tk()
    img = ImageTk.PhotoImage(Image.open(path))
    panel = tk.Label(root, image = img)
    panel.pack(side = "bottom", fill = "both", expand = "yes")
    root.mainloop()
    
于 2012-04-17T06:50:13.413 回答
6

path根据文件指向的格式,您的实际代码可能会返回错误。话虽如此,该类已经支持某些图像格式,例如 .gif、.pgm(如果 tk.TkVersion >= 8.6 则为 .png)PhotoImage

下面是一个显示示例:

莲娜 (.png)

或者如果tk.TkVersion < 8.6

莲娜 (.gif)

try:                        # In order to be able to import tkinter for
    import tkinter as tk    # either in python 2 or in python 3
except ImportError:
    import Tkinter as tk


def download_images():
    # In order to fetch the image online
    try:
        import urllib.request as url
    except ImportError:
        import urllib as url
    url.urlretrieve("https://i.stack.imgur.com/IgD2r.png", "lenna.png")
    url.urlretrieve("https://i.stack.imgur.com/sML82.gif", "lenna.gif")


if __name__ == '__main__':
    download_images()
    root = tk.Tk()
    widget = tk.Label(root, compound='top')
    widget.lenna_image_png = tk.PhotoImage(file="lenna.png")
    widget.lenna_image_gif = tk.PhotoImage(file="lenna.gif")
    try:
        widget['text'] = "Lenna.png"
        widget['image'] = widget.lenna_image_png
    except:
        widget['text'] = "Lenna.gif"
        widget['image'] = widget.lenna_image_gif
    widget.pack()
    root.mainloop()
于 2018-01-30T01:01:06.563 回答
5

它不是 python 2.7 的标准库。因此,为了使它们正常工作,如果您使用的是 Python 2.7,您应该首先下载 PIL 库:直接下载链接:http://effbot.org/downloads/PIL-1.1.7.win32-py2.7。 exe 安装后,请按照以下步骤操作:

  1. 确保您的script.py与您要显示的图像位于同一文件夹中。
  2. 编辑你的script.py

    from Tkinter import *        
    from PIL import ImageTk, Image
    
    app_root = Tk()
    
    #Setting it up
    img = ImageTk.PhotoImage(Image.open("app.png"))
    
    #Displaying it
    imglabel = Label(app_root, image=img).grid(row=1, column=1)        
    
    
    app_root.mainloop()
    

希望有帮助!

于 2014-10-18T14:33:16.390 回答
3

此代码对我有用,您还应该考虑该窗口中是否有任何其他按钮或标签并且您不使用.place()它将无法正常工作。

from Tkinter import*
from PIL import Image, ImageTk

img  = Image.open("path/x.png") 
photo=ImageTk.PhotoImage(img)
lab=Label(image=photo).place(x=50,y=50)
于 2019-09-02T16:58:02.623 回答
1

这是一个Python版本问题。如果您使用的是最新的,那么您的旧语法将不起作用并给您此错误。请遵循@Josav09 的代码,你会没事的。

于 2017-11-02T17:54:31.893 回答
1

只需将 jpg 格式的图像转换为 png 格式即可。它将 100% 工作。

于 2019-07-02T15:49:05.940 回答