I've been having a lot of trouble with the after method. What I want to do is show a series of images at the push of a button/press of a key -- each image should appear for 500ms, then be replaced by the next image, which should go on indefinitely (or end at the push/release of another button/key). It seems like I should be able to use after to accomplish this, but all my attempts so far have just resulted in recursion errors.

Here is the relevant code so far:

from Tkinter import *
import os

root = Tk()


frame = Frame(root)

# finds the screen size
sw = root.winfo_screenwidth()
sh = root.winfo_screenheight()

# expands window to full screen
root.geometry("%dx%d+0+0" % (sw, sh))

# images    
smiley = PhotoImage(file = 'Smiley-small.gif')
frowny = PhotoImage(file = 'Frowny-small.gif')

class judgmentPage:
    def __init__(self):
        # a frame inside the main frame that can be destroyed later
        self.innerframe = Frame(frame)
        self.innerframe.pack(expand=YES, fill=BOTH)
        # the canvas for displaying images
        self.canvas = Canvas(bg = 'white')
        self.canvas.pack(side=LEFT, expand = YES, fill = BOTH)
        self.button = Button(self.innerframe, text="Start Judgments", command=self.showJudgments)

    def showJudgments(self):
        self.image = self.canvas.create_image(500, 500, image = smiley, anchor=S)
        self.innerframe.after(500, self.update())
    def update(self):
        self.innerframe.after(500, self.showJudgments())

page = judgmentPage()

I've considered that it may have to do with the canvas -- maybe I am not using it correctly. Should I be using labels instead?


noob Aot

Oh, wow, boy do I feel silly. Here I thought that there must have been some crucial, core theory to the after or canvas methods that I was missing... and all I had to do was remove some parens, e.g.: self.innerframe.after(500, self.update) instead of self.innerframe.after(500, self.update()) :blush:

No need to blush, we all have made that mistake. Python allows functions to be passed to and from other functions, so all you have to remember is to use the function object and not the function call for the argument. Makes more sense once you look at it that way!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.