NewDeveloper404
NewDeveloper404

Reputation: 45

Why is my .after not working as intended?

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.

Upvotes: 1

Views: 67

Answers (1)

stovfl
stovfl

Reputation: 15513

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

Upvotes: 4

Related Questions