2012-04-13 18 views
13

Come si aggiunge un'immagine in Tkinter?Come aggiungere un'immagine in Tkinter?

questo mi ha dato un errore di sintassi:

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

vi consiglio di leggere il libro "Python e Tkinter programmazione". Ottimo libro, approfondito. Probabilmente li puoi trovare su eBay a prezzi inferiori. Questo presuppone che tu voglia veramente usare TKinter. Io raccomando Qt invece di Tkinter però – frankliuao

risposta

5

Non c'è un "Errore di sintassi" nel codice di cui sopra - è sia occurred in qualche altra linea (di cui sopra non è tutto il codice, in quanto vi sono nessuna importazione, né la dichiarazione della tua variabile path o hai qualche altro tipo di errore.

L'esempio precedente ha funzionato bene per me, testando l'interprete interattivo.

4

Dopo codice funziona sulla mia macchina

  1. probabilmente avete qualcosa che manca nel codice.
  2. verificare inoltre la codifica dei file di codice.
  3. assicuratevi di avere il pacchetto installato 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 bit (Intel)] su win32 14.May.2013

Questo ha funzionato per me, seguendo il codice sopra

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

Non è una lib di standard di python 2.7. Quindi, al fine di questi funzionino correttamente e se si sta utilizzando Python 2.7 è necessario scaricare la libreria PIL prima: diretto link per il download: http://effbot.org/downloads/PIL-1.1.7.win32-py2.7.exe Dopo averlo installato, attenersi alla seguente procedura:

  1. Accertarsi che lo script .py è a la stessa cartella con l'immagine si desidera mostrare.
  2. Modifica il tuo 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() 
    

Speranza che aiuta!

-1

Ecco un esempio per Python 3 che è possibile modificare per 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

E 'un problema versione di Python. Se stai utilizzando l'ultima, la tua vecchia sintassi non funzionerà e ti fornirò questo errore. Per favore segui il codice di @ Josav09 e starai bene.

0

Il codice effettivo può restituire un errore in base al formato del file path punti. Detto questo, alcuni formati di immagine come .gif, .pgm (e .png se tk.TkVersion> = 8.6) sono già supportati dalla classe PhotoImage.

Di seguito è riportato un esempio che visualizza:

Lenna (.png)

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