To insert an image in tkinter, you can do this:
import tkinter as tk
from tkinter import PhotoImage
class Window:
def __init__(self, image):
self.image = image
self.root = tk.Tk()
self.widgets()
self.root.mainloop()
def widgets(self):
self.img = PhotoImage(file=self.image)
label = tk.Label(self.root, image=self.img)
label.pack()
image = "001.png"
Window(image)
Ok, this is basic, but with some efforts we can make some nice apps, like the one we are going to make in the next paragraph.
Let’s make a slideshow
Let’s add some code to make a sort of slideshow. Well, it’s not exactly a slideshow, because you have to manually click on a button to make the images to change. With some little changes we can, after this example, see how to make it actually change at a certain pace.
import tkinter as tk
from tkinter import PhotoImage
from glob import glob
class Window:
import tkinter as tk
from tkinter import PhotoImage
from glob import glob
class Window:
def __init__(self, images: list):
self.images = images
self.imagepos = 0
self.root = tk.Tk()
self.widgets()
self.root.mainloop()
def widgets(self):
self.lab_image()
self.start_slide_button()
def lab_image(self):
self.img = PhotoImage(file=self.images[self.imagepos])
self.label = tk.Label(self.root, image=self.img)
self.label.pack()
def start_slide_button(self):
self.butstart = tk.Button(self.root,
text="Start slideshow",
command=self.slideshow)
self.butstart.pack()
def slideshow(self):
if self.imagepos < len(self.images) - 1:
self.imagepos += 1
else:
self.imagepos = 0
self.img = PhotoImage(file=self.images[self.imagepos])
self.label["image"] = self.img
images = glob("images\\*png")
win = Window(images)
Github repository
You can get the code and the images here:
https://github.com/formazione/tkinter_tutorial.git
Check for the file called image_slideshow.py
This is how the window shows up:

And here is another image

The images are made by an AI.
Bye
Subscribe to the newsletter for updates
Tkinter templatesTwitter: @pythonprogrammi - python_pygame
Claude's Games
1. Memory gameVideos
Speech recognition gamePygame's Platform Game