1

the looks of funktion I'm writing a code which shows an animation as Flipbook.

Therefore this code have to change the image and move the coordinate at same time. I wrote the code. However sometimes the animation is lagging.

I took a video of animation and then I found out the Problem.

In my code, the program changes coordinate first and then change the picture. Actually, this code has almost no problem, but sometimes it lags, then it will only move the picture. And after a while it will change the picture. Therefore I guess I have to use some kind of function which can change the image and move the image at same time. Is there any way to realize that?

Here is my code

import tkinter as tk

class App(object):

    def GUI(self):

        root=tk.Tk()
        root.geometry("600x600")

        self.pic001=tk.PhotoImage(file="picture001.png")
        self.pic002=tk.PhotoImage(file="picture002.png")

        self.canvas = tk.Canvas(bg="black", width=796, height=816)
        self.canvas.place(x=0, y=0)

        self.item=self.canvas.create_image(0, 0, image=self.pic001, anchor=tk.NW)

        self.canvas.move(self.item,-51,10)
        self.canvas.itemconfig(self.item, image = self.pic002)    

        root.mainloop()

app=App()

app.GUI()
Tomo
  • 15
  • 4
  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described. For starters, your indentation is incorrect. – Prune Jan 08 '19 at 19:43
  • what do you mean for animation ? no animation in your code – patel Jan 08 '19 at 20:09
  • Relevant [play-an-animated-gif-in-python-with-tkinter](https://stackoverflow.com/questions/43770847/play-an-animated-gif-in-python-with-tkinter) – stovfl Jan 08 '19 at 20:15
  • You cannot do both at the same time. No matter the order you do it in one has to execute before the other. Either moving first then image or change image then move. That said there is probably a better way to deal with canvas animation for your needs. For example you can draw a new image on the canvas in the new location and then delete the old one. – Mike - SMT Jan 08 '19 at 21:03

1 Answers1

1

Here is a simple animation script.

I drew 6 images and then copied 5 of them for the reversal bouncing.

I draw a new image each time instead of moving the image.

import tkinter as tk
from os import walk


class App(tk.Tk):
    def __init__(self):
        super().__init__()
        self.geometry("600x300")
        self.image_names = []
        self.index_tracker = 0
        self.location = 1
        self.canvas = tk.Canvas(self, width=2000)
        self.canvas.pack()
        for(dirpath, dirnames, filenames) in walk('./Flip/'):
            for name in filenames:
                self.image_names.append(tk.PhotoImage(file="{}{}".format(dirpath, name)))

        tk.Button(self, text='Start animation!', command=self.start_animation).pack()

    def start_animation(self):
        if self.location < 80:
            self.canvas.delete('all')
            if self.index_tracker < len(self.image_names):
                self.canvas.create_image(self.location * 10, 75, image=self.image_names[self.index_tracker])
                self.location += 1
                self.index_tracker += 1
                self.after(100, self.start_animation)
            else:
                self.index_tracker = 0
                self.start_animation()


if __name__ == "__main__":
    App().mainloop()

You can get the image files for this animation at my Github here.

Results:

enter image description here

Mike - SMT
  • 14,784
  • 4
  • 35
  • 79