Search code examples
python-3.xloopstkinterslideshowguizero

How do you make a slide show in Guizero/tkinter?


I am trying to make a slide show in python 3 with Guizero. I cant seem to make the next and back button work with time reset.

from guizero import *
app = App(bg='#121212',title='Guizero - slide show',width=500,height=500)

#list of images
images = ["img1.jpg", "img2.png", "img3.jpg", "img4.jpg"]

#sets what slide to start with
current_slide=0

#changes current slide displayed
def slideShow():
    global current_slide
    if len(images)-1>current_slide:
        current_slide+=1
        picture.image=images[current_slide]
    
    else:
        current_slide=0
        picture.image=images[current_slide]

#Function runs to change slide on button push
def slide_change(Change):
    global current_slide
    current_slide+=Change
    picture.image=images[current_slide]


#starter image    
picture = Picture(app, image=images[0])
#set picture size    
picture.width=app.width-100
picture.height=app.height-100


#Time based loop
picture.repeat(2000, slideShow)


#Buttons to change slides 
Back_button = PushButton(app, text='Back',command = lambda:slide_change(-1))
Back_button.text_color='white'

Next_button = PushButton(app, text='Next',command = lambda:slide_change(1))
Next_button.text_color='white'


app.display()

What I'm trying to do

  1. Move between slides on button press
  2. Reset time after slide change ( 2 seconds per slide)

What I'm using

  1. Python 3.7.9
  2. Guizero 1.3.0

Solution

  • from guizero import *
    app = App(bg='#121212',title='Guizero - slide show',width=500,height=500)
    
    #list of images
    images = ["img1.jpg", "img2.png", "img3.jpg", "img4.jpg"]
    
    #sets what slide to start with
    current_slide=0
    time_per_slide=2000
    
    #changes current slide displayed
    def slideShow():
        global current_slide
        if len(images)-1>current_slide:
            current_slide+=1
            picture.image=images[current_slide] 
        else:
            current_slide=0
            picture.image=images[current_slide]
    
    #Function runs to change slide on button push
    def nextImage():
        global current_slide
        if current_slide < len(images)-1: 
                current_slide+=1     
        else: 
            current_slide=0
        picture.image=images[current_slide]
        app.cancel(slideShow)
        #Restarts slide show
        picture.repeat(time_per_slide, slideShow)
        
    
    def previousImage():
        global current_slide
        if current_slide > 0:
          current_slide-=1      
        
        else:
            current_slide=len(images)-1    
        picture.image=images[current_slide]
        app.cancel(slideShow)
        #Restarts slide show
        picture.repeat(time_per_slide, slideShow)
        
        
    #starter image    
    picture = Picture(app, image=images[0])
    #set picture size    
    picture.width=app.width-100
    picture.height=app.height-100
    
    
    #Time based loop
    picture.repeat(time_per_slide, slideShow)
    
    
    #Buttons to change slides 
    Back_button = PushButton(app, text='Back', command =previousImage)
    Back_button.text_color='white'
    
    
    Next_button = PushButton(app, text='Next',command =nextImage)
    Next_button.text_color='white'
    
    app.when_closed=quit
    app.display()