Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
slideShow App
#1
Hi There,
I'm Rubberduck and an absolut beginner with python but I hope with your great help I become a solid python programmer and learn this fascinating language as quickly as I can.
My slideShow app should display some pictures but it seems that the code wouldn't work perfectly so far.

from itertools import cycle
import tkinter as tk

class App(tk.Tk):
    #Tk window/Label adjust to size of image
    def __init__(self, image_files, x, y, delay):

        tk.Tk.__init__(self)

        self.geometry('+{}+{}'.format(x, y))
        self.delay = delay

        self.pictures = cycle((tk.PhotoImage(file=image), image)
                              for image in image_files)
        self.pictures_display = tk.Label(self)
        self.picture_display.pack()
    def show_slides(self):
        #cycle through the images and dispay them

        img_object, img_name = next(self.pictures)
        self.picture_display.config(image=image_object)

        self.title(img_name)
        self.after(self.delay, self.show_slides)
    def run(self):
        self.mainloop()
    # set milliseconds time between slides
    delay = 3500
    # get a series of gif images have in the working folder
    # or use full path, or set directory to where the images are
    image_files = [
        '1.gif',
        '2.gif',
        '3.gif',
        '4.gif',
        '5.gif',
        '6.gif',
        '7.gif'
    ]

x = 100
y = 50
app = App(image_files, x, y, delay)
app.show_slides()
app.run()
My Terminal shows the following
Error:
File "/Users/damiankrebs1/Desktop/slideShow/main.py", line 43, in <module> app = App(image_files, x, y, delay) NameError: name 'image_files' is not defined
I already defined 'image_files' in line 31 so I cannot understand this error message.
It would be great if anyone could help. You also find my code here: github

Thanks for helping me.
Reply
#2
Line 31 is in the scope of your class. So it is not defined at the moment you are calling it. (Oh and besides, neither is "delay" defined.)
Reply
#3
Thanks for help.

Following your recommendation I reduce the indent so that both are defined but then I receive some other errors. Do you have a solution for this:

from itertools import cycle
import tkinter as tk

class App(tk.Tk):
    #Tk window/Label adjust to size of image
    def __init__(self, image_files, x, y, delay):

        tk.Tk.__init__(self)

        self.geometry('+{}+{}'.format(x, y))
        self.delay = delay

        self.pictures = cycle((tk.PhotoImage(file=image), image)
                              for image in image_files)
        self.pictures_display = tk.Label(self)
        self.picture_display.pack()
    def show_slides(self):
        #cycle through the images and dispay them

        img_object, img_name = next(self.pictures)
        self.picture_display.config(image=image_object)

        self.title(img_name)
        self.after(self.delay, self.show_slides)
    def run(self):
        self.mainloop()
    # set milliseconds time between slides

delay = 3500
# get a series of gif images have in the working folder
# or use full path, or set directory to where the images are
image_files = [
'1.gif',
'2.gif',
'3.gif',
'4.gif',
'5.gif',
'6.gif',
'7.gif'
]

x = 100
y = 50
app = App(image_files, x, y, delay)
app.show_slides()
app.run()
Error:
Traceback (most recent call last): File "/Users/damiankrebs1/Desktop/slideShow/main2.py", line 44, in <module> app = App(image_files, x, y, delay) File "/Users/damiankrebs1/Desktop/slideShow/main2.py", line 16, in __init__ self.picture_display.pack() File "/Users/damiankrebs1/opt/anaconda3/envs/venvUdemy/lib/python3.8/tkinter/__init__.py", line 2354, in __getattr__ return getattr(self.tk, attr) AttributeError: '_tkinter.tkapp' object has no attribute 'picture_display'
Reply
#4
Check line 16 and 21. Missing the s on pictures.
Modified a little but works.

#! /usr/bin/env python3
from itertools import cycle
import tkinter as tk
import os

class App(tk.Tk):
    #Tk window/Label adjust to size of image
    def __init__(self, image_files, x, y, delay):

        tk.Tk.__init__(self)

        self.geometry('+{}+{}'.format(x, y))
        self.delay = delay

        self.pictures = cycle((tk.PhotoImage(file=f'pics/{image}'), image)
                              for image in image_files)
        self.pictures_display = tk.Label(self)
        self.pictures_display.pack()

    def show_slides(self):
        #cycle through the images and dispay them

        img_object, img_name = next(self.pictures)
        self.pictures_display.config(image=img_object)

        self.title(img_name)
        self.after(self.delay, self.show_slides)

    def run(self):
        self.mainloop()
    # set milliseconds time between slides

delay = 3500
# get a series of gif images have in the working folder
# or use full path, or set directory to where the images are
image_files = os.listdir('pics')

x = 100
y = 50
app = App(image_files, x, y, delay)
app.show_slides()
app.run()
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#5
Awesome, many thanks @menator01

With your help it works great and the code look also cleaner with your 'pics' folder.

Best wishes,
Rubberduck
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020