How to run a function after a given amount of time in tkinter?

后端 未结 1 438
后悔当初
后悔当初 2021-01-22 17:18

So I have a .gif picture on a canvas in tkinter. I want this picture to change to another picture...but only for 3 seconds. and for it revert back to the original picture.

相关标签:
1条回答
  • 2021-01-22 17:39

    Tkinter widgets have a method named after which can be used to run a function after a specified period of time. To create an image and then change it three seconds later, you would do something like this:

    def setImage(self, filename):
        image = PhotoImage(file=filename)
        self.__leftImageCanvas.itemconfigure(self.__leftImage, image=image)
        self.__leftImageCanvas.image = image
    
    def startTurn(self):
        '''Set the image to "2h.gif", then change it to "b.gif" 3 seconds later'''
        setImage("2h.gif")
        self.after(3000, lambda: self.setImage("b.gif"))
    
    0 讨论(0)
提交回复
热议问题