I am trying to display an image to the window based on a phrase. If the character in the phrase matches the name of the picture, it should be displayed on the window. If the phrase has more than one character, the picture related to the character after the current character should be displayed to the right of the current character's picture. How would I be able to make it so that a new side-by side picture duo shows up, this time showing the picture related to the second character on the left and the third character on the right, and so on? I tried using .after() but I am not sure where to place it.
Also, when I run the following code I get an index out of range error unless I use the break statement.but I can't seem to figure out what it is. Maybe using the after() method will fix it?
import tkinter as tk
from PIL import ImageTk, Image, ImageDraw, ImageFont
import time
def open_image():
global i
global image
global img
global image2
global img2
if i < len(phrase):
if phrase[i] == " ":
image = Image.open("Rest.jpg")
img = ImageTk.PhotoImage(image)
panel['image'] = img
else:
image = Image.open(phrase[i] + ".jpg")
img = ImageTk.PhotoImage(image)
panel['image'] = img
if phrase[i + 1] != None and phrase[i + 1] != " ":
image2 = Image.open(phrase[i + 1] + ".jpg")
panel2['image2'] = img2
else:
image2 = Image.open("Rest1.jpg")
panel2['image2'] = img2
i += 1
window.after(2000, open_image)
else:
window.destroy()
# --- main ---
i = 0
phrase = " trac "
window = tk.Tk()
panel = tk.Label(window) # without image
panel.pack(side = "left", fill = "both", expand = "1", padx = 5, pady = 5)
panel2 = tk.Label(window)
panel2.pack(side = "left", fill = "both", expand = "1", padx = 5, pady = 5)
# use it after creating Label so it can set image
open_image()
window.mainloop()