How to do it...

First, we will create a working GUI that shuffles slides within a window frame using pure Python. Here is the working code and some screenshots of the results of running this code:

from tkinter import Tk, PhotoImage, Label 
from itertools import cycle
from os import listdir

class SlideShow(Tk):
# inherit GUI framework extending tkinter
def __init__(self, msShowTimeBetweenSlides=1500):
# initialize tkinter super class
Tk.__init__(self)

# time each slide will be shown
self.showTime = msShowTimeBetweenSlides

# look for images in current working directory
listOfSlides = [slide for slide in listdir()
if slide.endswith('gif')]

# cycle slides to show on the tkinter Label
self.iterableCycle = cycle((PhotoImage(file=slide), slide)
for slide in listOfSlides)

# create tkinter Label widget which can display images
self.slidesLabel = Label(self)

# create the Frame widget
self.slidesLabel.pack()


def slidesCallback(self):
# get next slide from iterable cycle
currentInstance, nameOfSlide = next(self.iterableCycle)

# assign next slide to Label widget
self.slidesLabel.config(image=currentInstance)

# update Window title with current slide
self.title(nameOfSlide)

# recursively repeat the Show
self.after(self.showTime, self.slidesCallback)


#=================================
# Start GUI
#=================================
win = SlideShow()
win.after(0, win.slidesCallback())
win.mainloop()

SlideShow.py

Here is another moment in time in the unfolding slide show:

While the slides sliding are truly impressive, the built-in capabilities of pure Python tkinter GUIs do not support the very popular .jpg format, so we have to reach out to another Python library. In order to use Pillow, we first have to install it using the pip command.

A successful installation looks as follows:

Pillow supports .jpg formats and, in order to use it, we to have to slightly change our syntax.

Without using Pillow, trying to display a .jpg  image results in the following error:

SlideShow_try_jpg.py

Using Pillow, we can display both .gif and .jpg files. After a successful installation of the Pillow package, we only have to make a few changes:

We search our folder for .jpg extensions and, instead of using the PhotoImage() class, we now use the ImageTk.PhotoImage() class. This enables us to display images with different extensions:

SlideShow_Pillow.py

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.57.126