gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 11:09:20 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com