【问题标题】:Why is my .after not working as intended?为什么我的 .after 没有按预期工作?
【发布时间】:2020-02-29 20:33:59
【问题描述】:

我正在尝试制作动画,动画只是一条从左到右移动的线,我正在尝试使其成为流畅的动画。但是当我按下 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()

我们将不胜感激。

【问题讨论】:

  • 你的输出是什么?您同时打印值,它们可以帮助您。 |看看你是如何处理.after 的,它不起作用很好——它会导致无限递归。 |尝试简单的time.sleep(1000) 以便循环可以执行这 10 次。
  • 你必须摆脱你的for ...循环。这必须替换为 10 个.after(... 调用。阅读Event-driven programming
  • 我不明白,你能给我举个例子吗?
  • 我还是不明白...
  • 您的 after 没有传递必需的参数。你需要像Root.after(1000, lambda: display_Click(event)) 这样的东西,但在这种情况下,我认为你要么使用不正确,要么需要做一些工作来管理你的函数中的按钮与之后。

标签: python python-3.x tkinter


【解决方案1】:

问题:使用.after 动画Line

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

【讨论】:

  • @Mike-SMT: 使用event=None 它正在使用bind(...command=.after(...。它是未定义的,call OP 在他的实际应用程序中使用。我知道 OP 想要移动 forwardbackward,因此该函数很可能会使用参数进行扩展。
  • [code] while Line_icon.winfo_x()
  • @Navil:尽管这样做,但您应该使用.update_idletasks() 而不是.update()。另请注意,while ... sleep(...) 会阻止 tkinter.mainloop(),这会导致 GUI 无响应。阅读While Loop Locks Application
猜你喜欢
  • 1970-01-01
  • 2019-08-28
  • 2014-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-04
  • 2021-01-03
  • 2018-11-08
相关资源
最近更新 更多