python - 为什么我的 .after 没有按预期工作?

标签 python python-3.x tkinter

我正在尝试制作动画,动画只是一条从左向右移动的线,我正在尝试使其成为流畅的动画。但是当我按下 Display_icon 时,它只是一直移动,并没有执行动画。我怎样才能使动画从 A 点流畅地移动到 B 点?

下面是一些代码:

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()

我们将不胜感激任何帮助。

最佳答案

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

关于python - 为什么我的 .after 没有按预期工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58694908/

相关文章:

python - 张量的形状为 [?, 0]——如何 reshape 为 [?,]

Python循环遍历CSV文件及其列

python - 在 python GUI 中使用坐标列表创建一条线

python - 如何在 scikit-learn 管道中向 Keras 网络添加纪元

python - 将文本拆分到关联表时无法显示文本列

python - Wagtail 根据登录用户的权限过滤页面子元素

python - 对于大型列表,如果一个点在 2 个点之间,如何快速匹配

python - 从 Treeview 中选择自动将字符串数字转换为整数

python - 如何在 tkinter 中输入动态延迟,以便屏幕上打印的数字在该延迟后出现

python - 在没有 Django REST 框架的情况下将 Swagger 文档添加到 Django