2012-04-13 1336 views
13

如何在Tkinter中添加图像?如何在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() 
+0

我建议你读的书 “Python和使用TKinter编程”。非常好的书,彻底。你可以在eBay上找到更低的价格。假设你真的想使用TKinter。虽然我推荐Qt,而不是Tkinter – frankliuao 2017-07-02 04:34:13

回答

5

没有“语法错误”在上面的代码 - 它在一些其他线路或者内容时发生(上面是不是所有的代码,因为有没有进口,既没有你的path变量的声明),或者你有一些其他的错误类型。

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

4

下面的代码工作在我的机器

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

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

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

  1. 确保您脚本.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() 
    

希望帮助!

-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

根据文件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()