0

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()
erondem
  • 482
  • 1
  • 11
  • 24
  • There are different ways of saving a reference. For example you can append all photos to a list. – Henry Yik Jul 16 '19 at 08:43
  • create list `self.photo_image = []` and `append()` images to this list. You can do it with `for`-loop. – furas Jul 16 '19 at 10:21
  • if you will keep it on list then it is easier to use it because you can use one function to show it `def show_image(self, image_index)` instead of `show_image1, show_image2, ..., show_image10000` – furas Jul 16 '19 at 10:23
  • So if I have 60 images, do I have to save them as reference at beginning in a list or dictionary. Then when I need lets say 30th picture, I'll take it back from list or dict. Is this how it should be ? – erondem Jul 17 '19 at 07:24

0 Answers0