Jun-17-2020, 04:02 AM
Hi everyone!
I'm trying as an experiment to build a very basic image viewer program with tkinter:
I'm trying as an experiment to build a very basic image viewer program with tkinter:
from tkinter import *
from PIL import ImageTk, Image
from glob import glob
index = 0
image_list = glob("Img\\*PNG")
image_obj = []
for x in image_list:
image_obj.append(ImageTk.PhotoImage(Image.open(x)))
def foward():
global index
if (index < len(image_obj)):
index = index + 1
visualizer.config(image=image_obj[index])
else:
pass
def backward():
global index
if (index > 0):
index = index - 1
visualizer.config(image=image_obj[index])
else:
pass
root = Tk()
visualizer = Label(root, image=image_obj[0])
visualizer.grid(row=0, column=0, columnspan=3)
back_button = Button(root, text='<<', command=foward)
quit_button = Button(root, text= 'EXIT', command=root.destroy())
foward_button = Button(root, text='>>', command=backward)
root.mainloop()And I'm running into the following problem:Error:Traceback (most recent call last):
File ".\photo_visualizer.py", line 10, in <module>
image_obj.append(ImageTk.PhotoImage(Image.open(x)))
File "C:\Users\tomi_\AppData\Local\Programs\Python\Python38-32\lib\site-packages\PIL\ImageTk.py", line 112, in __init__
self.__photo = tkinter.PhotoImage(**kw)
File "C:\Users\tomi_\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 4061, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Users\tomi_\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 3994, in __init__
raise RuntimeError('Too early to create image')
RuntimeError: Too early to create image
Exception ignored in: <function PhotoImage.__del__ at 0x03F816A0>
Traceback (most recent call last):
File "C:\Users\tomi_\AppData\Local\Programs\Python\Python38-32\lib\site-packages\PIL\ImageTk.py", line 118, in __del__
name = self.__photo.name
AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'I tried a lot of thing to make the PIL.PhotoImage metod work for me but nothing worked. Hope someone can guide me about this (or tell me about any other way to upload images to Tkinter, this was the only one that I found)
