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:
o se tk.TkVersion < 8.6
:
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()
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