我正在尝试使用Tkinter Text Widget制作动画。我希望它每1秒在Text小部件上编写每个帧。这是我目前的代码:
from tkinter import *
frames = ["-o","--o","---o"]
speed = 1000
def clear():
text.delete(0.0, END)
def movie(event):
text.delete(0.0, END)
for frame in range(len(frames)):
text.insert(END, frames[frame])
root.after(speed, clear)
root = Tk()
root.title("Animation")
root.minsize(400,400)
root.maxsize(width=root.winfo_screenwidth()-20, height=root.winfo_screenheight()-20)
text = Text(root, highlightcolor="black", highlightbackground="white",width=400, insertbackground="white", height=400, foreground="white", background="black", font="Courier")
text.pack()
root.bind("<Return>", movie)
但是,此代码的输出是
-o--o---o
而不是:
-o[wait a second][clear]--o[wait a second][clear]---o[wait a second][clear]
我该如何解决这个问题?
答案 0 :(得分:0)
我发现了我的问题! .after()函数几乎像返回函数一样工作。在那里,当被调用时,当前功能停止。这是我的固定代码:
from tkinter import *
frame = 0
frames = ["-o","--o","---o"]
speed = 1000
def clear():
text.delete(0.0, END)
def movie(event=""):
global frame
if frame < len(frames):
text.delete(0.0, END)
text.insert(END,frames[frame])
frame += 1
root.after(speed, movie)
def movie1(event):
global frame
frame = 0
movie("<Return>")
root = Tk()
root.title("Animation")
root.minsize(400,400)
root.maxsize(width=root.winfo_screenwidth()-20, height=root.winfo_screenheight()-20)
text = Text(root, highlightcolor="black", highlightbackground="white",width=400, insertbackground="white", height=400, foreground="white", background="black", font="Courier")
text.pack()
root.bind("<Return>", movie)