0

I was trying to make an image slideshow program with Tkinter and Python3. No errors, but not showing the images that are inside my chosen directory. The other libraries that I have use are: PIL, random and glob. Your help will be greatly appreciated.

My system:

Ubuntu 20.04 LTS

Here is the code:

import tkinter as Tk
from PIL import Image, ImageTk
import random
import glob

class gui:
    def __init__(self, mainwin):
        self.counter = 0
        self.mainwin = mainwin
        self.mainwin.title("Our Photos")
        self.colour()
        self.mainwin.configure(bg = "yellow")
        self.Frame = Tk.Frame(mainwin)
        self.img = Tk.Label(self.Frame)
        self.Frame.place(relheight = 0.85, relwidth = 0.9, relx = 0.05, rely = 0.05 )
        self.img.pack()
        self.pic()
    def colour(self):
        self.colours  =['gray47','gray48']

        c = random.choice(self.colours)
        self.mainwin.configure(bg = c)
        root.after(4000, self.colour)

    def pic(self):
        for name in glob.glob(r"/home/maheswar/Pictures/*"):
            self.pic_list = []
            val = name
            self.pic_list.append(val)
        
        if self.counter == len(self.pic_list) - 1:
            self.counter = 0
        else:
            self.counter == self.counter + 1
        
        self.file = self.pic_list[self.counter]
        self.load = Image.open(self.file)
        self.pic_width = self.load.size[0]
        self.pic_height = self.load.size[1]
        self.real_aspect = self.pic_width/self.pic_height
        self.calc_width = int(self.real_aspect * 800)  
        self.load2 = self.load.resize((self.calc_width, 800))
        self.render = ImageTk.PhotoImage(self.load2)
        self.img.config(image = self.render)
        self.img.image = self.render
        root.after(2000, self.pic) 
  


root = Tk.Tk()
myprog = gui(root)
root.geometry("1000x1000")
root.mainloop()
Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Jun 26 '22 at 02:36
  • @Community Sorry, but it needs to be there for better understanding. Thanks. – Maheswar KARAKKATTU KISHOR KUM Jun 26 '22 at 02:38
  • Maybe first use `print()` (and `print(type(...))`, `print(len(...))`, etc.) to see which part of code is executed and what you really have in variables. It is called `"print debuging"` and it helps to see what code is really doing. – furas Jun 26 '22 at 03:42
  • when I run code I get error `AttributeError: 'gui' object has no attribute 'pic_list'` – furas Jun 26 '22 at 03:44
  • You have big mistake - you create `self.pic_list = []` inside `for`-lopo - so every image in folder create again empty list and this removes previous values. You should create this list before `for`-loop. OR even simpler - use directly `self.pic_list = glob.glob(r"/home/maheswar/Pictures/*")` - you don't need `for`-loop for this – furas Jun 26 '22 at 03:45
  • another mistake - it has to be `=` instead of `==` in line `self.counter = self.counter + 1` or write it as `self.counter += 1` – furas Jun 26 '22 at 03:51

1 Answers1

0

I found two mistaces - which probably you could see if you would use print() to debug code

First: you create list self.pic_list = [] inside loop so you replace previous content and this way you can get only one list. But you don't event need this loop but directly

self.pic_list = glob.glob(r"/home/maheswar/Pictures/*")

Second: you need = instead of == in line self.counter = self.counter + 1 or even simpler

self.counter += 1

Full working code with small changes.

import tkinter as Tk
from PIL import Image, ImageTk
import random
import glob

class GUI:  # PEP8: `CamelCaseNames` for classes
    
    def __init__(self, mainwin):
        self.mainwin = mainwin
        self.mainwin.title("Our Photos")
        self.mainwin.configure(bg="yellow")  # PEP8: inside `()` use `=` without spaces

        self.counter = 0
        
        self.frame = Tk.Frame(mainwin)  # PEP8: `lower_case_names` for variables
        self.frame.place(relheight=0.85, relwidth=0.9, relx=0.05, rely=0.05)

        self.img = Tk.Label(self.frame)
        self.img.pack()

        self.pic_list = glob.glob("/home/maheswar/Pictures/*")  # no need prefix `r`
        self.colours = ['gray47', 'gray48']  # PEP8: space after `,`

        self.colour()
        self.pic()
        
    def colour(self):
        selected = random.choice(self.colours)
        self.mainwin.configure(bg=selected)
        root.after(4000, self.colour)

    def pic(self):

        filename = self.pic_list[self.counter]
        image = Image.open(filename)
        
        real_aspect = image.size[0]/image.size[1]
        width = int(real_aspect * 800)  
        
        image = image.resize((width, 800))
        
        self.photo = ImageTk.PhotoImage(image)
        self.img.config(image=self.photo)
        #self.img.image = self.render  no need if you use `self.` to keep PhotoImage

        self.counter += 1
        
        if self.counter >= len(self.pic_list):
            self.counter = 0

        root.after(2000, self.pic) 
  
# --- main ---

root = Tk.Tk()
myprog = GUI(root)
root.geometry("1000x1000")
root.mainloop()

PEP 8 -- Style Guide for Python Code

furas
  • 134,197
  • 12
  • 106
  • 148