I_Queerly_Belong_Here
I_Queerly_Belong_Here

Reputation: 15

How do I cycle through images in Tkinter?

I want to have the button's image cycle through all possible pizza images, but instead it throws the following error when I click on it:

self.config(image=next(self.images))
AttributeError: 'buttonInitialize' object has no attribute 'config'

Here's the code. I know it's ugly, sorry. I barely understand python classes, so I couldn't figure out how to factor the repetitive parts out

from tkinter import *
from itertools import cycle

class buttonInitialize:

    def __init__ (self,enabler):
        frame=Frame(enabler)
        frame.pack()

        pizzaList=[]
        pizzaList.append(PhotoImage(file="Cheese.png").zoom(10))
        pizzaList.append(PhotoImage(file="AvocadoWSauce.png").zoom(10))
        pizzaList.append(PhotoImage(file="AvocadoWCheese.png").zoom(10))

        self.images=cycle(pizzaList)
        self.printButton=Button(frame,image=pizzaList[0] ,command=self.nextPizza)
        self.printButton.pack(side=LEFT)

    def nextPizza(self):
        self.config(image=next(self.images))

root=Tk()
c=buttonInitialize(root)
root.mainloop()

Upvotes: 0

Views: 728

Answers (1)

Rob
Rob

Reputation: 143

Instead of self.config(), use self.printButton.config()

Upvotes: 1

Related Questions