如何在Tkinter中添加图像?

问题描述 投票:22回答:7

如何在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()
python user-interface tkinter
7个回答
10
投票

上面的代码中没有“语法错误” - 它要么在其他行中出现(上面不是所有的代码,因为没有导入,也没有你的path变量的声明)或者你有其他错误类型。

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


21
投票

Python 3.3.1 [MSC v.1600 32位(英特尔)]在win32 14.May.2013上

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

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

6
投票

以下代码适用于我的机器

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

4
投票

它不是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()

希望有所帮助!


3
投票

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

下面是一个显示示例:

Lenna (.png)

或者如果tk.TkVersion < 8.6

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

1
投票

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

enter image description here


0
投票

这是一个Python版本的问题。如果您使用的是最新版本,那么您的旧语法将无法正常工作并为您提供此错误。请关注@ Josav09的代码,你会没事的。


0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.