1

I'm trying to make a animation, The animations is simply a line moving from left to right and i'm trying to make it a fluid animation. But when i press Display_icon it just moves all the way and does not peform the animation. How can i make it so the animation moves fluedly from point A to B?

Heres some code:

from tkinter import *  # Import the tkinter module (For the Graphical User Interface)
from PIL import ImageTk, Image  # PILLOW for image formatting

width = 1920
height = 1080
RootGeo = str(width) + "x" + str(height)  # Make a def for RootGeo so the Root geometry isn't hardcoded

Root = Tk()

Settings_icon = Canvas(Root, bg="red", width=110, height=30)
Display_icon = Canvas(Root, bg="red", width=110, height=30)
Line_icon = Canvas(Root, bg="red", width=225, height=10)
Line = Line_icon.create_line(5, 6, 110, 6, width=5)


def Display_Click(event):
    print("[DISPLAY_CLICK] [INFO] [DEBUG] Display_icon has been clicked.")
    for i in range(10)
        Line_icon.move(Line, 5, 0)
        Root.after(1000, Display_Click)


def Settings_click(event):
    print("[SETTINGS_CLICK] [INFO] [DEBUG] Settings_icon has been clicked.")


def PicDir(PictureName):
    __DIRECTORY__ = "C:\\Users\\Gotta\\PythonProjects\\AutoCam\\Icons\\"
    __PICTURE_DIRECTORY__ = __DIRECTORY__ + PictureName

    print("[PICDIR] [INFO] [DEBUG] Photo Directory: ", __PICTURE_DIRECTORY__)
    return __PICTURE_DIRECTORY__


def MakeWindow():
    # -----Root_Attributes-----

    Root.geometry(RootGeo)
    Root.state("zoomed")
    Root.configure(bg="blue")

    # -----Root_Attributes, Root_Containers-----
    Settings_icon.create_text(57, 17, text="Settings", font="arial 20 bold")
    Settings_icon.bind("<ButtonRelease>", Settings_click)
    Settings_icon.place(x=5, y=5)

    Display_icon.create_text(57, 17, text="Display", font="arial 20 bold")
    Display_icon.bind("<ButtonRelease>", Display_Click)
    Display_icon.place(x=120, y=5)

    Line_icon.place(x=5, y=40)

    '''RUN COMMAND: py -3 tkinkertest.py'''
    # -----Root_Containers----- ### NOT WORKING ###

    Root.mainloop()


MakeWindow()

Any and all help would be very appreciated.

  • What is your output? You print values in the meantime, they could help you. | Looking how you did your `.after`, it's good it doesn't work - it would cause an infinite recursion. | Try just simple `time.sleep(1000)` so that the loop can execute those 10 times. – h4z3 Nov 04 '19 at 14:06
  • 1
    You have to get rid of your `for ...` loop. This have to be replaced with 10 `.after(...` calls. Read [Event-driven programming](https://stackoverflow.com/a/9343402/7414759) – stovfl Nov 04 '19 at 14:15
  • i dont think i understand, could you give me an example? – NewDeveloper404 Nov 04 '19 at 14:17
  • i still dont understand... – NewDeveloper404 Nov 04 '19 at 14:19
  • Your after is not passing a required argument. You need something like `Root.after(1000, lambda: display_Click(event))` however in this case I think you are either using after incorrectly or some work is needed to manage button vs after in your function. – Mike - SMT Nov 04 '19 at 14:36

1 Answers1

4

Question: Animate Line using .after

line_moves = 0

def Display_Click(event=None):
    print("[DISPLAY_CLICK] [INFO] [DEBUG] Display_icon has been clicked.")

    global line_moves

    line_moves += 1
    Line_icon.move(Line, 5, 0)

    if line_moves < 10:
        Root.after(1000, Display_Click)
    else:
        line_moves = 0

stovfl
  • 14,998
  • 7
  • 24
  • 51
  • 1
    @Mike-SMT: Using `event=None` it is working using `bind(...`, `command=` and `.after(...`. It is undefined, which `call` the OP is using in his real application. I know the OP want to move **foreward** and **backward**, therefore it is likely that the function becomes extended with a parameter. – stovfl Nov 04 '19 at 15:16
  • [code] while Line_icon.winfo_x() <= 40: print("LINE_ICON.WININFO: " + str(Line_icon.winfo_x())) Line_icon.move(Line, 3, 0) Line_icon.update() sleep(0.0001)[/code] – NewDeveloper404 Nov 05 '19 at 12:11
  • 1
    @Navil: Feel free to do so, but you should use `.update_idletasks()` instead of `.update()`. Also be aware, `while ... sleep(...)` blocks the `tkinter.mainloop()` which leads to a **not responsive GUI**. Read [While Loop Locks Application](https://stackoverflow.com/questions/28639228/python-while-loop-locks-application) – stovfl Nov 05 '19 at 13:32