An image can be loaded from disk and displayed within a label (tk.Label
or ttk.Label
) by using the tk.PhotoImage
class:
|
import tkinter as tk
|
|
from tkinter import ttk
|
|
|
|
root = tk.Tk()
|
|
root.title("Display Image in Tk")
|
|
root.geometry("400x300")
|
|
# Load image from disk.
|
|
image = tk.PhotoImage(file="image.png")
|
|
# Display it within a label.
|
|
label = ttk.Label(image=image)
|
|
label.pack()
|
|
root.mainloop()
|
The image.png
file must be in the same directory (actually the same working directory) as the code. The following example does not require any extra files because it includes the image data encoded via base64 inside a variable:
|
import tkinter as tk
|
|
from tkinter import ttk
|
|
from base64 import b64decode
|
|
|
|
root = tk.Tk()
|
|
root.title("Display Image in Tk")
|
|
root.geometry("400x300")
|
|
# Load image data from memory.
|
|
raw_image = "iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAYAAADDPmHLAAAACXBIWXMAAAsTAAALEwEAmpwYAAAM3klEQVR4nO2dC3BU1RnHabVqO+3YOr7a2qljO46Ote3UmYoVElDIbjbZ7G42m3dCePtANHRqIqABFELkZXgEEEPCI0BVHFRaH1ihtj5QMW8gIeERICE8krCb3fvYe/frdxKwIAF27717z97N+c/8Jskw2fOd7/8/Z8+9uxuGDGFiYmJiYmJiYmIygl4HuKaKg7s28xC/iYOnqngo3CTA/CoBlm/ioQK/fx2/rsafFyMv4r8X4NexG0V4uArgZtr1M4UoNPruKhEmo6mb0cx6hEeTQSn4+2fwsT7DUJRu5MFWAfBT2nNkukC4wq9bz4MdzVmHHEUgnGzgQNrIwVdIMQbk97TnP2hVKcB9aMb8DTx0biDGUGI9B40YwIINvfBz2j2JepHn83UCZK7joG49aX4EgTVJGIYtlWxX0F6rAX5Q6YNcZD82GiIdrHNHhReG0u5bVGgdHrywoYcqSWMNRoUPtlVwcCftHhpSr3rhjgqf/EaFLwBGZq1X9lV45VmlANfT7qkhVATw/bU+KCjHxq0lDYwWvPJ+3BEept3fiFa5G25B498r9wYgKumV/Ti/WSTktHsdcSr3QeyaXvn4a9ioaGeNV/6oohdup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vG4AtBpIqFgGmIw5Ze7LBngN7YWQgiR3Z72mKHPR30xm5PL6I9d6ZzsuFOYLehKfoSwBBk0J470znZbamL7bY00BNbUprHZXHdTnvuTOeEhsxDQF9SI+MvkDP1y5bkwhCkgn64emNjx9xAe95MF8hqTV2UZE0FvbBaXWbac2b6jhITXfOsiS7QAxyrkPZ8mQZQYqITQ5ACYSchpZT2XJkuo0SLc15iQgqEFYvzFdrzZLqC+kPghHCRkOCMzL9FzPR/WTAECRY0Kwwkmp1xtOfHFIQsFgeGIBm0xdFrNpuvpz03piBlMTvmWeIdoBlmB3v+N5rQtEVamB9vdritVutttOfDpEBms21evNkOKgjEx9uM9ddImS5WvMlWrNh8k/052vUzaSCTyZZjikvymU02CI4kj8lkT6VdN5OGSkhI+KXJlLT6SkEwxdm8+HWZxWJhL/9GqxITE39kHm2z4q7wvDnOtpwYTr6PH22Pd7lcur7xlYmJiYmJiYmJyaD6HynKMa5WAh67AAAAAElFTkSuQmCC"
|
|
image = tk.PhotoImage(data=b64decode(raw_image))
|
|
label = ttk.Label(image=image)
|
|
label.pack()
|
|
root.mainloop()
|
An image might be displayed within a button also. See Button in Tk (tkinter) for an example.