【发布时间】:2016-04-28 15:13:34
【问题描述】:
是否有可能以任何方式在 Tkinter 或 Python 中制作文本动画? (虽然程序是基于 Tkinter 的)
例如,可能文本的字符一个接一个地出现,就像您输入的速度非常快。有没有办法做到这一点?
非常感谢。
【问题讨论】:
-
是的,有可能。使用
root.after重复运行更改标签上文本的函数。
标签: python animation text tkinter
是否有可能以任何方式在 Tkinter 或 Python 中制作文本动画? (虽然程序是基于 Tkinter 的)
例如,可能文本的字符一个接一个地出现,就像您输入的速度非常快。有没有办法做到这一点?
非常感谢。
【问题讨论】:
root.after 重复运行更改标签上文本的函数。
标签: python animation text tkinter
示例 - 在标签上显示当前时间。
after() 在 1 秒后运行 update_time,update_time 使用 after() 在 1 秒后再次运行。这样update_time会被多次调用,可以多次改变标签。
import tkinter as tk # Python 3.x
import time
# function which changes time on Label
def update_time():
# change text on Label
lbl['text'] = time.strftime('Current time: %H:%M:%S')
# run `update_time` again after 1000ms (1s)
root.after(1000, update_time) # function name without ()
# create window
root = tk.Tk()
# create label for current time
lbl = tk.Label(root, text='Current time: 00:00:00')
lbl.pack()
# run `update_time` first time after 1000ms (1s)
root.after(1000, update_time) # function name without ()
#update_time() # or run first time immediately
# "start the engine"
root.mainloop()
【讨论】: