Bild in Python 3.12.1 anzeigen lassen?

1 Antwort

Beispiel:

import tkinter as tk

root = tk.Tk()
image = tk.PhotoImage(file="path\\to\\image")
label = tk.Label(root, image=image)
label.pack()

root.mainloop()

Einmal davon abgesehen, dass der Bildpfad passen muss, werden von tkinter nur bestimmte Bildformate unterstützt (GIF, PGM, PNG, PPM). Wenn du JPG-Bilder darstellen möchtest, musst du dir eine zusätzliche Bibliothek wie Pillow zu Hilfe nehmen.

from PIL import Image, ImageTk

# ...
image = Image.open(file="path\\to\\image")
photo = ImageTk.PhotoImage(image)
label = tk.Label(root, image=photo)
# ...