I want to show many pictures in my gui application. But according to Here I need to save those pictures as reference to my class. but I am confused how should I do it with many pictures?
This does not show any image on the opened Window
from tkinter import *
import tkinter
import os
from PIL import Image, ImageTk
CURRENT_DIR = os.path.dirname(os.path.realpath(__file__)) + "/"
class GUI():
def __init__(self,file_list):
self.file_list = file_list
self.root=Tk()
self.canvas = Canvas(self.root, width=480, height=800)
self.canvas.pack()
def image_opener(self, filename):
opened_image = Image.open(CURRENT_DIR + "images/gui/" + filename)
photo_image = ImageTk.PhotoImage(opened_image)
return photo_image
def show_image1(self):
img = self.image_opener(files["img1"])
self.canvas.create_image(0, 0, image=img, anchor = NW, state = NORMAL)
def show_image2(self):
img = self.image_opener(files["img2"])
self.canvas.create_image(0, 0, image=img, anchor = NW, state = NORMAL)
def show_screens(self):
self.show_image1()
self.show_image2()
self.root.mainloop()
files = {
"img1":"image1.png",
"img2":"image2.png",
"img3":"image3.png",
"img4":"image4.png"
}
gui = GUI(files)
gui.show_screens()
When I change it to this it shows pictures in the window but is there any elegant way to do it with many pictures (maybe around 75) ?
from tkinter import *
import tkinter
import os
from PIL import Image, ImageTk
CURRENT_DIR = os.path.dirname(os.path.realpath(__file__)) + "/"
class GUI():
def __init__(self,file_list):
self.file_list = file_list
self.root=Tk()
self.canvas = Canvas(self.root, width=480, height=800)
self.canvas.pack()
self.opened_image1 = Image.open(CURRENT_DIR + "images/gui/image1")
self.photo_image1 = ImageTk.PhotoImage(self.opened_image1)
self.opened_image2 = Image.open(CURRENT_DIR + "images/gui/image2")
self.photo_image2 = ImageTk.PhotoImage(self.opened_image2)
def show_image1(self):
self.canvas.create_image(0, 0, image=self.photo_image1, anchor = NW, state = NORMAL)
def show_image2(self):
self.canvas.create_image(0, 0, image=self.photo_image2, anchor = NW, state = NORMAL)
def show_screens(self):
self.show_image1()
self.show_image2()
self.root.mainloop()
files = {
"img1":"image1.png",
"img2":"image2.png",
"img3":"image3.png",
"img4":"image4.png"
}
gui = GUI(files)
gui.show_screens()